iOS數據持久化——數據歸檔

##一、數據歸檔(Archive)編碼

使用屬性列表能夠持久化數據,可是這種保存的方式是明文的不能達到文件的隱祕性。iOS中還提供了一種持久化的方法叫作數據歸檔,使用 NSKeyedArchiver (歸檔) 和 NSKeyedUnarchiver (解歸檔)類完成。這種方式能夠對數據進行編碼爲二進制的形式保存,從而達到數據的隱祕性。要歸檔的數據必須實現**<NSCoding>協議,<NSCoding>協議包含encodeWithCoder:**編碼方法 和 **initWithCoder:**解碼方法。atom

  • ###1.1 經常使用Foundation類的數據歸檔 經常使用的Foundation類都實現了<NSCoding>協議,全部能夠直接歸檔,包含可變類型。歸檔的過程就是將數據進行編碼保存,使用 NSKeyedArchiver 進行編碼歸檔。
//一、建立文件保存路徑
   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];

輸入圖片說明

  • ###1.2 經常使用Foundation類的數據解歸檔 解檔就是將編碼的數據進行反編碼爲原來的類型數據,使用 NSKeyedUnarchiver 完成解檔。
NSMutableArray *objs = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    NSLog(@"%@",objs);
  • ###1.3自定義的對象歸檔 有時候咱們須要本地保存本身定義的一些對象數據,這些對象並無實現<NSCoding>協議,因此在歸檔數據前要先去實現<NSCoding>協議中的方法,在協議方法中對自定義對象屬性的編碼和解碼。以下實現自定義的User類對象的歸檔:
一、.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];
  • ###1.4 自定義的對象解歸檔
// 解檔
    User *userData = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    NSLog(@"user:%@",userData);
  • ###1.5 將數據轉爲NSData歸檔

歸檔數據時能夠直接將該類型數據歸檔,也能夠將這些類型的數據編碼爲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);
相關文章
相關標籤/搜索