归档

一:什么是归档

对象归档是将对象归档以文件的形式保存到磁盘中(也称为序列化,持久化)使用的时候读取该文件的保存路径读取文件的内容(也称为接档,反序列化)

对象归档的文件是保密的磁盘上无法查看文件中的内容,而属性列表是明文的可以查看

1,使用archiveRootObject进行简单地归档和解档(对一个对象进行归档)

- (void)viewDidLoad {

    [super viewDidLoad];

    NSString *str = @"afa";

    NSString *astr = @"111";

    NSArray *Array = [NSArray arrayWithObjects:str, astr, nil];

    //归档

    NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *filename = [Path stringByAppendingPathComponent:@"test"];

    [NSKeyedArchiver archiveRootObject:Array toFile:filename];

    

    //解档

    NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile: filename];

    str = [arr objectAtIndex:0];

    astr =  [arr objectAtIndex:1];

    NSLog(@"str:%@",str);

    NSLog(@"astr:%@",astr);

 }

2,对多个对象的归档(对基本类型数据)

归档(写入数据)

 step1:准备数据

  1. CGPoint point = CGPointMake(1.0, 2.0);  
  2. NSString *info = @"坐标原点";  
  3. NSInteger value = 10;  
  4. NSString *multiHomePath = [NSHomeDirectory() stringByAppendingPathComponent:@"multi.archiver"];  
  5. NSMutableData *data = [[NSMutableData alloc]init];  
  6. NSKeyedArchiver *archvier = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];  

step2:对多个对象进行归档  

  1. [archvier encodeCGPoint:point forKey:@"kPoint"];  
  2. [archvier encodeObject:info forKey:@"kInfo"];  
  3. [archvier encodeInteger:value forKey:@"kValue"];  
  4. [archvier finishEncoding];  
  5. [data writeToFile:multiHomePath atomically:YES]; 

解档(路径中获得数据构造NSKeyedUnarchiver实例,使用decodeXXXForKey方法获得文件中的对象。)

  1. NSMutableData *dataR = [[NSMutableData alloc]initWithContentsOfFile:multiHomePath];  
  2. NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:dateR];  
  3. CGPoint pointR = [unarchiver decodeCGPointForKey:@"kPoint"];  
  4. NSString *infoR = [unarchiver decodeObjectForKey:@"kInfo"];  
  5. NSInteger valueR = [unarchiver decodeIntegerForKey:@"kValue"];  
  6. [unarchiver finishDecoding];  
  7. NSLog(@"%f,%f,%@,%d",pointR.x,pointR.y,infoR,valueR); 

3,对自定义对象进行归档

step1:自定义一个实体类Archive

Archive.h

  1. #import <Foundation/Foundation.h>  
  2.  
  3. @interface Archive : NSObject  
  4. @property (copy,nonatomic) NSString *name;  
  5. @property NSInteger age;  
  6. @property (copy,nonatomic) NSString *address;  
  7. @property (copy,nonatomic) UIImage *photo;  
  8.   
  9. @end 

Archive.m

  1. #import "Archive.h"  
  2. #define kNameKey @"NameKey"  
  3. #define kAgeKey @"AgeKey"  
  4. #define kAddress @"AddressKey"  
  5. #define kPhotoKey @"PhotoKey"  
  6.   
  7. @implementation Archive  
  8.   
  9. #pragma mark - NSCoding  
  10. - (void)encodeWithCoder:(NSCoder *)aCoder {  
  11.     [aCoder encodeObject:_name forKey:kNameKey];  
  12.     [aCoder encodeInteger:_age forKey:kAgeKey];  
  13.     [aCoder encodeObject:_address forKey:kAddress];  
  14.     [aCoder encodeObject:_photo forKey:kPhotoKey];  
  15. }  
  16.   
  17. - (id)initWithCoder:(NSCoder *)aDecoder {  
  18.     if (self = [super init]) {  
  19.         _name = [aDecoder decodeObjectForKey:kNameKey];  
  20.         _age = [aDecoder decodeIntegerForKey:kAgeKey];  
  21.         _address = [aDecoder decodeObjectForKey:kAddress];  
  22.         _photo = [aDecoder decodeObjectForKey:kPhotoKey];  
  23.     }  
  24.     return self;  
  25. }  
  26.   
  27. #pragma mark - NSCoping  
  28. - (id)copyWithZone:(NSZone *)zone {  
  29.     Archive *copy = [[[self class] allocWithZone:zone] init];  
  30.     copy.name = [self.name copyWithZone:zone];  
  31.     copy.age = self.age;  
  32.     copy.address = [self.address copyWithZone:zone];  
  33.     copy.photo = self.photo;  
  34.     return copy;  
  35. }  
  36. @end  

 

Archive实现了三个委托方法1)encodeWithCoder: 2)initWithCoder:  3)copyWithZone:

1)encodeWithCoder

Encodes the receiverusing a given archiver

通过一个给定的archiver把消息接收者进行编码。

当接收到encodeObject消息的时候,类终端encodeWithCoder方法被调用。

2)initWithCoder

Returns an objectinitialized from data in a given unarchiver. (required)

从一个给定unarchiver的数据中返回一个初始化对象。

3)copyWithZone

Returnsa new instance that’s a copy of the receiver

返回消息接收者的一个复制的新实例。

SDK的概念就是这样,下面看看这个自定义类归档的具体代码,其实和多个对象的归档是一样的。。。

 

step2:归档:

  1. //保存图片与归档  
  2. - (IBAction)save:(id)sender {  
  3.       
  4.     //准备数据  
  5.     NSString *name = @"小杨在玩iOS";  
  6.     NSInteger age = 22;  
  7.     NSString *address = @"你猜我在哪~";  
  8.     UIImage *photo = [UIImage imageNamed:@"loginman.jpg"];  
  9.     //存储数据到类  
  10.     Archive *archivingData = [[Archive alloc] init];  
  11.     archivingData.name = name;  
  12.     archivingData.age = age;  
  13.     archivingData.address = address;  
  14.     archivingData.photo = photo;  
  15.       
  16.     //归档  
  17.     NSMutableData *data = [[NSMutableData alloc] init];  
  18.     NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];  
  19.   
  20.     [archiver encodeObject:archivingData forKey:kArchivingDataKey]; // archivingDate的encodeWithCoder  
  21. 方法被调用  
  22.     [archiver finishEncoding];  
  23.     //写入文件  
  24.     [data writeToFile:self.archivingFilePath atomically:YES];  
  25. }  

 

step3:接档:

  1. - (IBAction)loadArchive:(id)sender {  
  2.     NSData *data = [[NSMutableData alloc] initWithContentsOfFile:self.archivingFilePath];  
  3.     NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];  
  4.       
  5.     //获得类  
  6.     Archive *archivingData = [unarchiver decodeObjectForKey:kArchivingDataKey];// initWithCoder方法被调用  
  7.     [unarchiver finishDecoding];  
  8.       
  9.     //读取的数据  
  10.     NSString *name = archivingData.name;  
  11.     NSInteger age = archivingData.age;  
  12.     NSString *address = archivingData.address;  
  13.     self.imageView.image = archivingData.photo;  
  14.     NSLog(@"%@||%d||%@",name,age,address);  
  15. }  

 

posted @ 2015-08-24 16:01  IT菜鸟来袭  阅读(213)  评论(0编辑  收藏  举报