iOS 归档archive使用方法

归档是一种很常用的文件储存方法,几乎任何类型的对象都能够被归档储存,文件将被保存成自定
义类型的文件,相对于NSUserDefault具有更好的保密性。
 
1.使用archiveRootObject进行单个对象的归档
使用NSKeyedArichiver进行归档、NSKeyedUnarchiver进行解档,这种方式会在写入、读出数据之
前对数据进行序列化、反序列化操作。
 
归档:
//获取根目录
NSString *homeDictionary = NSHomeDirectory();
//添加储存的文件名
NSString *homePath  = [homeDictionary
stringByAppendingPathComponent:@"atany.archiver"];
//归档一个字符串
BOOL flag = [NSKeyedArchiver archiveRootObject:@"归档" toFile:homePath];
这种方式可以对字符串、数字等进行归档,当然也可以对NSArray与NSDictionary进行归档。返回
值Flag标志着是否归档成功,YES为成功,NO为失败。
 
解档:
NSString* str=[NSKeyedUnarchiver unarchiveObjectWithFile:homePath];
使用NSKeyedUnarchiver进行接档(反序列化)。
 
2.对多个对象的归档
同样是使用NSKeyedArchiver进行归档,不同的是同时归档多个对象,这里举例放入了一个
CGPoint点、字符串、整数(当然很多类型都可以的,例如UIImage、float等等),使用encode
方法进行归档,最后通过writeToFile方法写入文件。
 
归档:写入数据
//准备数据 
CGPoint point = CGPointMake(1.0, 2.0); 
NSString *info = @"坐标原点"; 
NSInteger value = 10; 
//获取Home目录并创建multi.archiver文件
NSString *multiHomePath = [NSHomeDirectory()
stringByAppendingPathComponent:@"multi.archiver"]; 
//创建NSMutableData对象 用来存储归档数据
NSMutableData *data = [[NSMutableData alloc]init]; 
NSKeyedArchiver *archvier = [[NSKeyedArchiver alloc]
initForWritingWithMutableData:data]; 
//对多个对象进行归档 
[archvier encodeCGPoint:point forKey:@"kPoint"]; 
[archvier encodeObject:info forKey:@"kInfo"]; 
[archvier encodeInteger:value forKey:@"kValue"]; 
[archvier finishEncoding]; 
//写入文件
[data writeToFile:multiHomePath atomically:YES]; 

 

解档:从路径中获得数据构造NSKeyedUnarchiver实例,使用decodeXXXForKey方法获得文件中的对
象。
NSMutableData *dataR = [[NSMutableData alloc]initWithContentsOfFile:multiHomePath]; 
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]
initForReadingWithData:dateR]; 
CGPoint pointR = [unarchiver decodeCGPointForKey:@"kPoint"]; 
NSString *infoR = [unarchiver decodeObjectForKey:@"kInfo"]; 
NSInteger valueR = [unarchiver decodeIntegerForKey:@"kValue"]; 
[unarchiver finishDecoding]; 
NSLog(@"%f,%f,%@,%d",pointR.x,pointR.y,infoR,valueR); 

 

3.对自定义对象进行归档
自定义对象,应用范围很广,因为它对应着MVC中的Model层,即实体类。在程序中,我们会在
Model层定义很多的entity,例如User,Teacher等。
 
那么对自定义对象的归档显得重要得多,因为很多情况下我们需要在Home键之后保存数据,在程序
恢复时重新加载,那么,归档便是一个好的选择。
 
首先自定义一个实体类,Archive。
 
Archive.h
#import <Foundation/Foundation.h> 
 
@interface Archive : NSObject 
@property (copy,nonatomic) NSString *name; 
@property NSInteger age; 
@property (copy,nonatomic) NSString *address; 
@property (copy,nonatomic) UIImage *photo; 
 
@end 
 
Archive.m
#import "Archive.h" 
#define kNameKey @"NameKey" 
#define kAgeKey @"AgeKey" 
#define kAddress @"AddressKey" 
#define kPhotoKey @"PhotoKey" 
 
@implementation Archive 
@synthesize name = _name; 
@synthesize age = _age; 
@synthesize address = _address; 
@synthesize photo = _photo; 
 
#pragma mark - NSCoding 
- (void)encodeWithCoder:(NSCoder *)aCoder { 
    [aCoder encodeObject:_name forKey:kNameKey]; 
    [aCoder encodeInteger:_age forKey:kAgeKey]; 
    [aCoder encodeObject:_address forKey:kAddress]; 
    [aCoder encodeObject:_photo forKey:kPhotoKey]; 
} 
 
- (id)initWithCoder:(NSCoder *)aDecoder { 
    if (self = [super init]) { 
        _name = [aDecoder decodeObjectForKey:kNameKey]; 
        _age = [aDecoder decodeIntegerForKey:kAgeKey]; 
        _address = [aDecoder decodeObjectForKey:kAddress]; 
        _photo = [aDecoder decodeObjectForKey:kPhotoKey]; 
    } 
    return self; 
} 
 
#pragma mark - NSCoping 
- (id)copyWithZone:(NSZone *)zone { 
    Archive *copy = [[[self class] allocWithZone:zone] init]; 
    copy.name = [self.name copyWithZone:zone]; 
    copy.age = self.age; 
    copy.address = [self.address copyWithZone:zone]; 
    copy.photo = self.photo; 
    return copy; 
} 
@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
返回消息接收者的一个复制的新实例。
 
归档:
//保存图片与归档 
- (IBAction)save:(id)sender { 
    //准备数据 
    NSString *name = @"Exile"; 
    NSInteger age = 22; 
    NSString *address = @"Address"; 
    UIImage *photo = [UIImage imageNamed:@"login.jpg"]; 
    //存储数据到类 
    Archive *archivingData = [[Archive alloc] init]; 
    archivingData.name = name; 
    archivingData.age = age; 
    archivingData.address = address; 
    archivingData.photo = photo; 
    //归档 
    NSMutableData *data = [[NSMutableData alloc] init]; 
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]
initForWritingWithMutableData:data]; 
    // archivingDate的encodeWithCoder 方法被调用 
    [archiver encodeObject:archivingData forKey:kArchivingDataKey]; 
    [archiver finishEncoding]; 
    //写入文件 
    [data writeToFile:self.archivingFilePath atomically:YES]; 
} 
 
解档:
- (IBAction)loadArchive:(id)sender { 
    NSData *data = [[NSMutableData alloc]
initWithContentsOfFile:self.archivingFilePath]; 
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]
initForReadingWithData:data]; 
    //获得类 
    //initWithCoder方法被调用 
    Archive *archivingData = [unarchiver decodeObjectForKey:kArchivingDataKey];
    [unarchiver finishDecoding]; 
 
    //读取的数据 
    NSString *name = archivingData.name; 
    NSInteger age = archivingData.age; 
    NSString *address = archivingData.address; 
    self.imageView.image = archivingData.photo; 
    NSLog(@"%@||%d||%@",name,age,address); 
} 
posted @ 2016-02-07 14:31  death3721  阅读(568)  评论(0编辑  收藏  举报