Archiver是持久化數據的一種方式,他跟 Plist的區別在於他能持久化自定義對象。但他沒Plist那麼方便。
Archiver默認能持久化的數據有NSNumber,NSArray,NSDictionary,NSString,NSData,因為這幾個對象已經實現了
1.
//恢復歸檔文件為對象 -(id)initWithCoder:(NSCoder *)aDecoder //歸檔,使對象持久化 -(void)encodeWithCoder:(NSCoder *)aCoder
如下 ,我們首先獲取歸檔文件的路徑
#pragma mark 獲取文件路徑
- (NSString *) filePath
{
NSArray *dirPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES);
NSString *dirPath=dirPaths[0];
NSString *filePath=[dirPath stringByAppendingPathComponent:@"aa.archiver"];
return filePath;
}
2.系統默認對象如何歸檔(NSNumber,NSArray,NSDictionary,NSString,NSData)
#pragma mark 歸檔/恢復 Array對象
- (void) savearray
{
NSString *filePath=[self filePath];
//
// NSArray *arr=@[@"ttt",@"BBB",@25];
// [NSKeyedArchiver archiveRootObject:arr toFile:filePath];
//
NSArray *arr1=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%@",arr1);
}#pragma mark 歸檔/恢復 Dictionary對象
- (void) saveDic
{
NSString *filePath=[self filePath];
// NSDictionary *dict=@{@"name":@"lean",@"age":@25};
// BOOL flag=[NSKeyedArchiver archiveRootObject:dict toFile:filePath];
// NSLog(@"%d",flag);
NSDictionary *dict2=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%@",dict2);
}#importTIP: 不管是encode還是decode 都是根據對象的類型去選用不同的方法。如@interface Person : NSObject @property (nonatomic,copy) NSString *name; @property (nonatomic,assign) int age; + (Person *) initWithName:(NSString *)name andAge:(int) age; @end #import "Person.h" @implementation Person + (Person *) initWithName:(NSString *)name andAge:(int) age { Person *p=[[Person alloc] init]; p.name=name; p.age=age; return p; } -(void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:self.name forKey:@"name"]; [aCoder encodeInt:self.age forKey:@"age"]; } -(id)initWithCoder:(NSCoder *)aDecoder { [self setName:[aDecoder decodeObjectForKey:@"name"]]; [self setAge:[aDecoder decodeIntForKey:@"age"]]; return self; } @end
encodeInt:forkey: encodeDouble:forkey: encodeFloat:forkey:
decodeObjectForKey: decodeIntForKey: decodeDoubleForKey:
NSKeyedArchiver archiveRootObject:toFile:
NSKeyedUnarchiver unarchiveObjectWithFile:
分別是對需要歸檔。恢復的對象進行操作的兩個類
定義完了Person類後,在需要歸檔的地方調用如下:
#pragma mark 歸檔/恢復 自定義對象
- (void) savePerson
{
NSString *filePath=[self filePath];
Person *p=[Person initWithName:@"lean" andAge:22];
BOOL flag=[NSKeyedArchiver archiveRootObject:p toFile:filePath];
Person *p2=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%d-%d",flag,p2.age);
}
4.假設該對象是某個對象子類,這裡我們建立一個叫Student類作為Person的子類
#import "Person.h" @interface Student : Person @property (nonatomic ,assign) int no; + (Student *) initWithName:(NSString *)name andAge:(int) age andNO:(int) no; @end
-(id)initWithCoder:(NSCoder *)aDecoder
{
if (self=[super initWithCoder:aDecoder]) {
[self setNo:[aDecoder decodeIntForKey:@"no"]];
}
return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[super encodeWithCoder:aCoder];
[aCoder encodeInt:self.no forKey:@"no"];
}#pragma mark 歸檔/恢復 自定義子類對象
- (void) saveStudent
{
NSString *filePath=[self filePath];
Student *p=[Student initWithName:@"lean" andAge:22 andNO:150133];
BOOL flag=[NSKeyedArchiver archiveRootObject:p toFile:filePath];
Student *p2=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%d-%@",flag,p2.name);
}