##一、數據歸檔(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);