對象歸檔,就是把內存中對象持久化。
對象解檔,就是把持久化的對象讀取到內存。
oc中對象歸檔解檔大致分為以下幾種方法:
從數量上可以分為:
對單個對象歸檔解檔 對多個對象歸檔解檔
從對象的存在形式上可以分為:
系統對象歸檔解檔 自定義對象歸檔解檔
接下來逐一編寫代碼做簡單實現
1.單個系統對象歸檔及解檔:
void SingleSystemObject(){ NSArray *arr = [NSArray arrayWithObjects:@1,@"你好",@3, nil]; NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"array.txt"]; //對象歸檔 BOOL success = [NSKeyedArchiver archiveRootObject:arr toFile:filePath]; if (success) { NSLog(@"歸檔成功"); } //對象解檔 NSArray *arr2 = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; for (NSString *str in arr2) { NSLog(@"%@",str); } }
2. 多個系統對象歸檔及解檔
void MultiSystemObject(){ NSArray *arr = [NSArray arrayWithObjects:@1,@2,@"中文", nil]; NSInteger integer = 10; BOOL archiverBool = YES; /*------對象歸檔--------------------*/ NSMutableData *data = [NSMutableData data]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; //對多個對象編碼歸檔,key是解檔時對象的標識符,可任意編寫 //encode有多個重載,但並不是任何系統對象都有重載方法 [archiver encodeObject:arr forKey:@"arr"]; [archiver encodeInteger:integer forKey:@"integer"]; [archiver encodeBool:archiverBool forKey:@"archiverBool"]; //完成編碼 [archiver finishEncoding]; NSString *filepath = [NSHomeDirectory() stringByAppendingPathComponent:@"1.txt"]; //將data以原子操作方式寫入文件 BOOL success = [data writeToFile:filepath atomically:YES]; if (success) { NSLog(@"歸檔成功"); } /*------對象歸檔--------------------*/ /*------對象解檔--------------------*/ //將文件數據加載入NSData對象 NSData *data2 = [NSData dataWithContentsOfFile:filepath]; //使用NSData對象初始化接檔器 NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data2]; //根據歸檔時的類型和key解檔對應數據 NSArray *arr2 = [unarchiver decodeObjectForKey:@"arr"]; NSInteger integer2 = [unarchiver decodeIntegerForKey:@"integer"]; BOOL unarchiveBool = [unarchiver decodeBoolForKey:@"archiveBool"]; NSLog(@"%@",arr2); NSLog(@"%ld",integer2); NSLog(@"%d",unarchiveBool); /*------對象接檔--------------------*/ }
3.自定義對象歸檔及解檔
自定義對象實現可歸檔解檔,需遵守NSCoding協議,該協議下有兩個方法需要實現,分別是
歸檔編碼
- (void)encodeWithCoder:(NSCoder *)aCoder
歸檔解碼
- (id)initWithCoder:(NSCoder *)aDecoder
此兩個方法中實現自定義對象中自定義成員變量歸檔時的編碼和解碼方式,類似於多系統對象歸檔的實現方式,即將成員變量按類型編碼並制定key,解碼時根據key按類型解碼並賦值給成員變量,最終將對象返回。簡單代碼實現如下:
Person.m
@interface Person : NSObject <NSCoding> @property (copy,nonatomic)NSString *name; @property (assign,nonatomic)NSInteger age; @end @implementation Person - (void)encodeWithCoder:(NSCoder *)aCoder{ [aCoder encodeObject:self.name forKey:@"name"]; [aCoder encodeInteger:self.age forKey:@"age"]; } - (id)initWithCoder:(NSCoder *)aDecoder{ // self = [super init]; // if (self) { self.name = [aDecoder decodeObjectForKey:@"name"]; self.age = [aDecoder decodeIntegerForKey:@"age"]; // } return self; } - (NSString *)description{ NSString *desc = [NSString stringWithFormat:@"name = %@,age = %ld",self.name,self.age]; return desc; } @end
歸檔解檔代碼:
void UserDefinedObject(){ Person *p = [[Person alloc] init]; p.name = @"yangys"; p.age = 10; //歸檔 NSString *filepath = [NSHomeDirectory() stringByAppendingPathComponent:@"person.txt"]; BOOL success = [NSKeyedArchiver archiveRootObject:p toFile:filepath]; if (success) { NSLog(@"歸檔成功"); } //解檔 Person *person = [NSKeyedUnarchiver unarchiveObjectWithFile:filepath]; NSLog(@"%@",person); }
自定義多個對象的歸檔及解檔如多個系統對象的歸檔及解檔。
這裡值得一提的是,系統對象的歸檔及解檔中,只有遵守了NSCoding協議或其子協議的系統對象才可進行歸檔及解檔。