iOS archive(归档)的总结

一.概念解释:

归档是一种很常用的文件储存方法,几乎任何类型的对象都能够被归档储存(实际上是一种文件保存的形式),使用NSKeyedArichiver进行归档、NSKeyedUnarchiver进行接档,这种方式会在写入、读出数据之前对数据进行序列化、反序列化操作。可以对字符串、数字等进行归档,当然也可以对NSArrayNSDictionary进行归档。返回值Flag标志着是否归档成功,YES为成功,NO为失败。

二.单一归档/解挡:

  1. NSString *homeDictionary = NSHomeDirectory();//获取根目录  
  2. NSString *homePath  = [homeDictionary stringByAppendingPathComponent:@"atany.archiver"];//添加储存的文件名  
  3. BOOL flag = [NSKeyedArchiver archiveRootObject:@”归档” toFile:homePath];//归档一个字符串  
  1. [NSKeyedUnarchiver unarchiveObjectWithFile:homePath]  
三.多个对象的归档/解档:
  1. //准备数据  
  2. CGPoint point = CGPointMake(1.0, 2.0);  
  3. NSString *info = @"坐标原点";  
  4. NSInteger value = 10;  
  5. NSString *multiHomePath = [NSHomeDirectory() stringByAppendingPathComponent:@"multi.archiver"];  
  6. NSMutableData *data = [[NSMutableData alloc]init];  
  7. NSKeyedArchiver *archvier = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];  
  8.   
  9. //对多个对象进行归档  
  10. [archvier encodeCGPoint:point forKey:@"kPoint"];  
  11. [archvier encodeObject:info forKey:@"kInfo"];  
  12. [archvier encodeInteger:value forKey:@"kValue"];  
  13. [archvier finishEncoding];  
  14. [data writeToFile:multiHomePath atomically:YES];  
  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);  
三,自定义对象归档/解档:
  1. 首先我们需要,自定义一个实体类,Archive
  2. Archive.h
  3. #import <Foundation/Foundation.h>  
  4.   
  5. @interface Archive : NSObject  
  6. @property (copy,nonatomic) NSString *name;  
  7. @property NSInteger age;  
  8. @property (copy,nonatomic) NSString *address;  
  9. @property (copy,nonatomic) UIImage *photo;  
  10.   
  11. @end  
  12. Archive.m
  13. #import "Archive.h"  
  14. #define kNameKey @"NameKey"  
  15. #define kAgeKey @"AgeKey"  
  16. #define kAddress @"AddressKey"  
  17. #define kPhotoKey @"PhotoKey"  
  18.   
  19. @implementation Archive  
  20. @synthesize name = _name;  
  21. @synthesize age = _age;  
  22. @synthesize address = _address;  
  23. @synthesize photo = _photo;  
  24.   
  25. #pragma mark - NSCoding  
  26. - (void)encodeWithCoder:(NSCoder *)aCoder {  
  27.     [aCoder encodeObject:_name forKey:kNameKey];  
  28.     [aCoder encodeInteger:_age forKey:kAgeKey];  
  29.     [aCoder encodeObject:_address forKey:kAddress];  
  30.     [aCoder encodeObject:_photo forKey:kPhotoKey];  
  31. }  
  32.   
  33. - (id)initWithCoder:(NSCoder *)aDecoder {  
  34.     if (self = [super init]) {  
  35.         _name = [aDecoder decodeObjectForKey:kNameKey];  
  36.         _age = [aDecoder decodeIntegerForKey:kAgeKey];  
  37.         _address = [aDecoder decodeObjectForKey:kAddress];  
  38.         _photo = [aDecoder decodeObjectForKey:kPhotoKey];  
  39.     }  
  40.     return self;  
  41. }  
  42.   
  43. #pragma mark - NSCoping  
  44. - (id)copyWithZone:(NSZone *)zone {  
  45.     Archive *copy = [[[self class] allocWithZone:zone] init];  
  46.     copy.name = [self.name copyWithZone:zone];  
  47.     copy.age = self.age;  
  48.     copy.address = [self.address copyWithZone:zone];  
  49.     copy.photo = self.photo;  
  50.     return copy;  
  51. }  
  52. @end  
Archive实现了三个委托方法1)encodeWithCoder: 2)initWithCoder:  3)copyWithZone:

  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]; // archivingDateencodeWithCoder  
  21. 方法被调用  
  22.     [archiver finishEncoding];  
  23.     //写入文件  
  24.     [data writeToFile:self.archivingFilePath atomically:YES];  
  25. }  
  26. 接档:
  27. - (IBAction)loadArchive:(id)sender {  
  28.     NSData *data = [[NSMutableData alloc] initWithContentsOfFile:self.archivingFilePath];  
  29.     NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];  
  30.       
  31.     //获得类  
  32.     Archive *archivingData = [unarchiver decodeObjectForKey:kArchivingDataKey];// initWithCoder方法被调用  
  33.     [unarchiver finishDecoding];  
  34.       
  35.     //读取的数据  
  36.     NSString *name = archivingData.name;  
  37.     NSInteger age = archivingData.age;  
  38.     NSString *address = archivingData.address;  
  39.     self.imageView.image = archivingData.photo;  
  40.     NSLog(@"%@||%d||%@",name,age,address);  
  41. }  

备注: 要将一个自定义的类进行归档,那么类里面的每个属性都必须是可以被归档的,如果是不能归档的类型,我们可以把他转化为NSValue进行归档,然后在读出来的时候在转化为相应的类。

内容地址:http://www.cnblogs.com/ios8/p/ios-archive.html  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值