NSString、NSArray、NSData、NSDictionary 都實作了 NSCoding 協議,可直接呼叫 writeToFile 歸檔。那麼 Objective-C 自訂的物件型別該怎麼辦?做法是:先讓自訂類別實作 NSCoding 協議,重寫 encodeWithCoder: 與 initWithCoder: 兩個方法,然後透過 NSKeyedArchiver 轉成 NSData,再用 NSData 的 writeToFile 寫入檔案;也可以把轉出的 NSData 放進 NSArray 或 NSDictionary 再呼叫 writeToFile,如此就能歸檔包裝了自訂型別的資料與字典。讀取時,用 NSKeyedUnarchiver 反歸檔成物件,或透過 NSArray 的 arrayWithContentsOfFile、NSDictionary 的 dictionaryWithContentsOfFile 取回陣列或字典,再從中取出序列化過的自訂物件(即 NSData 形式),最後用 NSKeyedUnarchiver 反歸檔還原成物件即可。
------------------------------------------------測試程式碼------------------------------------------------
NSString *str = @"hello";
[str writeToFile:@"/Users/lili/Desktop/str.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSArray *arr = [[NSArray alloc]initWithObjects:@"lili",@"tata", nil];
[arr writeToFile:@"/Users/lili/Desktop/arr.txt" atomically:YES];
NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[@"lili",@"男"] forKeys:@[@"name",@"sex"]];
[dict writeToFile:@"/Users/lili/Desktop/dict.txt" atomically:YES];
Person *parent = [[Person alloc]init];
parent.age = 40;
parent.name = @"lili";
Person *child = [[Person alloc]init];
child.age = 22;
child.name = @"linian";
parent.child = child;
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:parent];
[data writeToFile:@"/Users/lili/Desktop/data.txt" atomically:YES];
Person *people = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/lili/Desktop/data.txt"];
NSLog(@"%@",people);
NSDictionary *dictPerson = [NSDictionary dictionaryWithObjects:@[[NSKeyedArchiver archivedDataWithRootObject:parent],[NSKeyedArchiver archivedDataWithRootObject:child]] forKeys:@[@"parent",@"child"]];
[dictPerson writeToFile:@"/Users/lili/Desktop/dictPerson.txt" atomically:YES];
NSDictionary *pdict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/lili/Desktop/dictPerson.txt"];
Person *tPerson = [NSKeyedUnarchiver unarchiveObjectWithData:[pdict objectForKey:@"parent"]];
NSLog(@"%@",tPerson);
NSArray *arrPerson = [[NSArray alloc]initWithObjects:[NSKeyedArchiver archivedDataWithRootObject:parent],[NSKeyedArchiver archivedDataWithRootObject:parent], nil];
[arrPerson writeToFile:@"/Users/lili/Desktop/arrPerson.txt" atomically:YES];
NSArray *pArr = [NSArray arrayWithContentsOfFile:@"/Users/lili/Desktop/arrPerson.txt"];
Person *aPerson = [NSKeyedUnarchiver unarchiveObjectWithData:[pArr objectAtIndex:0]];
NSLog(@"%@",aPerson);
------------------------------------------------Person 類別------------------------------------------------
#import <Foundation/Foundation.h>
@interface Person : NSObject<NSCoding>
@property(strong,nonatomic)Person *child;
@property(assign,nonatomic)int age;
@property(copy,nonatomic)NSString *name;
@end
#import "Person.h"
@implementation Person
-(NSString *)description
{
return [NSString stringWithFormat:@"{age:%d,name:%@,child:%@}",self.age,self.name,self.child];
}
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeInt:self.age forKey:@"age"];
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.child forKey:@"child"];
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
if(self = [super init]){
self.age = [aDecoder decodeIntForKey:@"age"];
self.name = [aDecoder decodeObjectForKey:@"name"];
self.child = [aDecoder decodeObjectForKey:@"child"];
}
return self;
}
@end