归档和解档:即将数据写入文件和从文件中读取数据。
此处以plist文件为例说明,
一、plist文件使用时的注意事项:
1.plist文件中仅支持写入Array,Dictionary,Boolean,Data,Date,Number,String类型。
2.如果想要将自定义类的对象数据写入plist文件,则需要将对象修改为NSData类型写入。
二、归档步骤:
1.自定义的类必须遵守NSCoding协议,重写其归档和解档的方法。
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder;
2.使用[NSKeyedArchiver archivedDataWithRootObject:p1]方法将p1对象转化为NSData类型。
3.通过NSArray调用arrayWithObjects方法写入plist文件中。
三、解档步骤:
1.从plist文件中读取内容
2.调用 [NSKeyedUnarchiver unarchiveObjectWithData:arr[i]]方法将NSData对象转化为Person对象。
四、plist基本类型的写入:
1.代码如下:
1 //注意:此处的路径为应用程序束的路径,即工程在开发人员本地的路径 2 NSString *filePath = @"/Users/zsx/Documents/zsxzsx/JieDang_GuiDang/JieDang_GuiDang/nameList.plist"; 3 4 //写入的数据的类型为NSArray,plist根结构类型只包括NSArray和NSDictionary两种。 5 NSArray *arr = [[NSArray alloc]initWithObjects:@"zhangwuji",@"qiaofeng",@"shipotian",@"moyuan",@"yehua", nil]; 6 7 BOOL flag = [arr writeToFile:filePath atomically:YES]; 8 if (flag) { 9 NSLog(@"写入成功.."); 10 }
2.打印结果为:
2017-02-25 15:18:25.917848 JieDang_GuiDang[24127:1160011] 写入成功..
3.plist内容中的数据为:
五、自定义类型数据的写入
1.首先介绍自定义类Person,其.h代码为:
1 @interface Person : NSObject<NSCoding> 2 @property(nonatomic,copy)NSString *name; 3 @property(nonatomic,assign)NSInteger age; 4 - (instancetype)initWithName:(NSString *)name withAge:(NSInteger)age; 5 @end
2.Person.m的代码为:
1 @implementation Person 2 3 - (instancetype)initWithName:(NSString *)name withAge:(NSInteger)age { 4 self = [super init]; 5 if (self) { 6 _age = age; 7 _name = name; 8 } 9 return self; 10 } 11 12 - (NSString *)description 13 { 14 return [NSString stringWithFormat:@"name:%@ age:%ld", self.name,self.age]; 15 } 16 17 #pragma mark- NSCoding 18 - (void)encodeWithCoder:(NSCoder *)aCoder { 19 [aCoder encodeObject:self.name forKey:@"name"];//key和value尽量保持一致 20 [aCoder encodeInteger:self.age forKey:@"age"]; 21 } 22 - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder { 23 self.name = [aDecoder decodeObjectForKey:@"name"]; 24 self.age = [aDecoder decodeIntegerForKey:@"age"]; 25 return self; 26 } 27 @end
3.自定义对象数据写入:
1 void writeToPlist(NSString *filePath){ 2 //1.遵循NSCoding协议,才能进行归档和解档 3 Person *p1 = [[Person alloc]initWithName:@"zhangwuji" withAge:20]; 4 Person *p2 = [[Person alloc]initWithName:@"lilei" withAge:18]; 5 Person *p3 = [[Person alloc]initWithName:@"hanmeimei" withAge:17]; 6 7 //2.将对象转化为NSData对象 8 NSData *data1 = [NSKeyedArchiver archivedDataWithRootObject:p1]; 9 NSData *data2 = [NSKeyedArchiver archivedDataWithRootObject:p2]; 10 NSData *data3 = [NSKeyedArchiver archivedDataWithRootObject:p3]; 11 12 //3.写入文件 13 NSArray *arr2 = [NSArray arrayWithObjects:data1,data2,data3, nil]; 14 BOOL flag = [arr2 writeToFile:filePath atomically:YES]; 15 if (flag) { 16 NSLog(@"自定义对象写入成功..."); 17 } 18 }
4.自定义对象的读取:
1 void readFromPlist(NSString *filePath) { 2 //1.从plist文件中读取内容 3 NSArray *arr = [NSArray arrayWithContentsOfFile:filePath]; 4 5 for (int i = 0; i < arr.count; i++) { 6 //2.将NSData对象转化为Person对象 7 Person *p = [NSKeyedUnarchiver unarchiveObjectWithData:arr[i]]; 8 NSLog(@"%@",p); 9 } 10 }
5.调用:
1 int main(int argc, const char * argv[]) { 2 @autoreleasepool { 3 //如果想要写入自定义类型对象的数据,则需要将自定义类型的对象转化为Data类型,才允许写入 4 NSString *fPath = @"/Users/zsx/Documents/zsxzsx/JieDang_GuiDang/JieDang_GuiDang/objectList.plist"; 5 6 //写入plist文件 7 writeToPlist(fPath); 8 9 //从plist文件中读取 10 readFromPlist(fPath); 11 } 12 return 0; 13 }
6.打印结果为:
2017-02-25 15:18:25.918924 JieDang_GuiDang[24127:1160011] 自定义对象写入成功...
2017-02-25 15:18:25.919242 JieDang_GuiDang[24127:1160011] name:zhangwuji age:20
2017-02-25 15:18:25.919302 JieDang_GuiDang[24127:1160011] name:lilei age:18
2017-02-25 15:18:25.919341 JieDang_GuiDang[24127:1160011] name:hanmeimei age:17
7.plist中显示的二进制数据。。