##一、数据归档(Archive)编码
使用属性列表能够持久化数据,可是这种保存的方式是明文的不能达到文件的隐秘性。iOS中还提供了一种持久化的方法叫作数据归档,使用 NSKeyedArchiver
(归档) 和 NSKeyedUnarchiver
(解归档)类完成。这种方式能够对数据进行编码为二进制的形式保存,从而达到数据的隐秘性。要归档的数据必须实现**<NSCoding>
协议,<NSCoding>协议包含encodeWithCoder:
**编码方法 和 **initWithCoder:
**解码方法。atom
//一、建立文件保存路径 NSString *dicPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *filePath = [dicPath stringByAppendingPathComponent:@"data.archive"]; //二、设置保存的数据 NSMutableArray *infos = [NSMutableArray array]; [infos addObject:@"One"]; [infos addObject:@100]; [infos addObject:[NSDate date]]; //三、将数据归档到指定路径 [NSKeyedArchiver archiveRootObject:infos toFile:filePath];
NSMutableArray *objs = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; NSLog(@"%@",objs);
一、.h中设置属性并实现<NSCoding>协议 @interface User : NSObject <NSCoding> @property(assign, nonatomic) NSInteger userID; @property(copy, nonatomic) NSString *userName; @property(copy, nonatomic) NSString *userPassword; @end
二、.m文件中实现协议方法,encodeWithCoder: 和 initWithCoder:完成对属性数据的编码和解码。 // 给数据编码 - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeInteger:_userID forKey:@"userID"]; [aCoder encodeObject:_userName forKey:@"userName"]; [aCoder encodeObject:_userPassword forKey:@"userPassword"]; } // 给数据解码 - (instancetype)initWithCoder:(NSCoder *)aDecoder { if (self = [super init]) { self.userID = [[aDecoder decodeObjectForKey:@"userID"] integerValue]; self.userName = [aDecoder decodeObjectForKey:@"userName"]; self.userPassword = [aDecoder decodeObjectForKey:@"userPassword"]; } return self; }
三、将自定义对象归档到指定文件路径中 // 设置路径 NSString *dicPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *filePath = [dicPath stringByAppendingPathComponent:@"user"]; // 建立对象设置数据 User *user = [[User alloc] init]; user.userID = 1000001; user.userName = @"kingde"; user.userPassword = @"123456789"; // 归档自定义对象,归档会调用encodeWithCoder:方法 [NSKeyedArchiver archiveRootObject:user toFile:filePath];
// 解档 User *userData = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; NSLog(@"user:%@",userData);
归档数据时能够直接将该类型数据归档,也能够将这些类型的数据编码为NSData类型的数据后再进行持久保存(NSData保存的是二进制的数据)。code
NSKeyedArchiver的**archivedDataWithRootObject:
**:方法将对象数据编码为NSData类型数据。对象
NSKeyedUnarchiver的**unarchiveObjectWithData:
**:方法将NSData类型数据解码为原类型数据。图片
NSString *dicPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *filePath = [dicPath stringByAppendingPathComponent:@"object"]; User *user = [[User alloc] init]; user.userID = 1000002; user.userName = @"kitty"; user.userPassword = @"98754321"; // 一、使用NSKeyedArchiver将自定义的对象数据编码为NSData对象 NSData *data = [NSKeyedArchiver archivedDataWithRootObject:user]; // 二、写入数据到指定路径中 [data writeToFile:filePath atomically:YES]; // 三、获取保存的NSData数据 NSData *objData = [NSData dataWithContentsOfFile:filePath]; // 四、将NSData数据解码为自定义的对象 User *userObj = [NSKeyedUnarchiver unarchiveObjectWithData:objData]; NSLog(@"%@",userObj);