zoukankan      html  css  js  c++  java
  • 使用NSKeyedArichiver进行归档、NSKeyedUnarchiver进行解档

    一、使用archiveRootObject进行简单的归档

    使用NSKeyedArichiver进行归档、NSKeyedUnarchiver进行接档,这种方式会在写入、读出数据之前对数据进行序列化、反序列化操作。

    归档:

    1. NSString *homeDictionary = NSHomeDirectory();//获取根目录  
    2. NSString *homePath  = [homeDictionary stringByAppendingPathComponent:@"atany.archiver"];//添加储存的文件名  
    3. BOOL flag = [NSKeyedArchiver archiveRootObject:@”归档” toFile:homePath];//归档一个字符串  

    这种方式可以对字符串、数字等进行归档,当然也可以对NSArray与NSDictionary进行归档。返回值Flag标志着是否归档成功,YES为成功,NO为失败。

    接档:

    1. [NSKeyedUnarchiver unarchiveObjectWithFile:homePath]  

    使用NSKeyedUnarchiver进行接档(反序列化)。

    这种归档的方式存在一个缺点:只能把一个对象归档进一个文件中,那么怎么对多个对象进行归档呢?

    二、对多个对象的归档

    同样是使用NSKeyedArchiver进行归档,不同的是同时归档多个对象,这里我们举例放入了一个CGPoint点、字符串、整数(当然很多类型都可以的,例如UIImage、float等等),使用encodeXXX方法进行归档,最后通过writeToFile方法写入文件。

    归档:写入数据

    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];  



    接档:从路径中获得数据构造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);  

    可以看出对多个对象进行归档还是挺方便的,这里又出现一个问题,这里的对象都是基本类型数据,那么怎么对自己定义类生成的实例对象进行归档呢?

    三、对自定义对象进行归档

    自定义对象,应用范围很广,因为它对应着MVC中的Model层,即实体类。在程序中,我们会在Model层定义很多的entity,例如User,Teacher。。

    那么对自定义对象的归档显得重要的多,因为很多情况下我们需要在Home键之后保存数据,在程序恢复时重新加载,那么,归档便是一个好的选择。

    首先我们需要,自定义一个实体类,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. @synthesize name = _name;  
    9. @synthesize age = _age;  
    10. @synthesize address = _address;  
    11. @synthesize photo = _photo;  
    12.   
    13. #pragma mark - NSCoding  
    14. - (void)encodeWithCoder:(NSCoder *)aCoder {  
    15.     [aCoder encodeObject:_name forKey:kNameKey];  
    16.     [aCoder encodeInteger:_age forKey:kAgeKey];  
    17.     [aCoder encodeObject:_address forKey:kAddress];  
    18.     [aCoder encodeObject:_photo forKey:kPhotoKey];  
    19. }  
    20.   
    21. - (id)initWithCoder:(NSCoder *)aDecoder {  
    22.     if (self = [super init]) {  
    23.         _name = [aDecoder decodeObjectForKey:kNameKey];  
    24.         _age = [aDecoder decodeIntegerForKey:kAgeKey];  
    25.         _address = [aDecoder decodeObjectForKey:kAddress];  
    26.         _photo = [aDecoder decodeObjectForKey:kPhotoKey];  
    27.     }  
    28.     return self;  
    29. }  
    30.   
    31. #pragma mark - NSCoping  
    32. - (id)copyWithZone:(NSZone *)zone {  
    33.     Archive *copy = [[[self class] allocWithZone:zone] init];  
    34.     copy.name = [self.name copyWithZone:zone];  
    35.     copy.age = self.age;  
    36.     copy.address = [self.address copyWithZone:zone];  
    37.     copy.photo = self.photo;  
    38.     return copy;  
    39. }  
    40. @end  



    Archive类有四个字段(名字、年纪、地址、头像),除了年纪为整型之外,其他的都看作Object。

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

    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的概念就是这样,下面看看这个自定义类归档的具体代码,其实和多个对象的归档是一样的。。。

    归档:

    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. }  

    接档:

    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. }  

    我们save之后loadArchive一次

    打出结果为:

    2013-11-04 19:29:41.743TestArchives[16708:c07]小杨在玩iOS||22||你猜我在哪~

    获取成功。

  • 相关阅读:
    LeetCode
    LeetCode
    static,final,包,访问修饰符,内部类
    抽象类和抽象方法接口和多态
    抽象类和抽象方法
    memcache的使用、版本使用和相关配置
    apache mysql无法启动解决办法
    thinkphp实现文件的下载
    xampp 出现403 无法访问问题(已解决)
    Thinkphp使用phpexcel导入文件并写入数据库
  • 原文地址:https://www.cnblogs.com/seth-chen/p/4374295.html
Copyright © 2011-2022 走看看