iOS 归档的使用

1、面向对象的程序在运行的时候会创建一个复杂的对象图,经常要以二进制的方法序列化这个对象图,这个过程叫做Archiving. 二进制流可以通过网络或写入文件中

例:NSKeyedArchiver
========================================================*/
NSString *str = @”abc”;
NSString *astr = @”efg”;
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];

str = @”a”;
astr = @”";

//加载数据
NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile: filename];
str = [arr objectAtIndex:0];
astr =  [arr objectAtIndex:1];

NSLog(@”str:%@”,str);
NSLog(@”astr:%@”,astr);

2、在nsstring 的类的定义中已经添加了协议<nscoding> 即他是实现了nscoding 代理的方法的。

@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>

NScoding 是一个协议,主要有下面两个方法

-(id)initWithCoder:(NSCoder *)coder;//从coder中读取数据,保存到相应的变量中,即反序列化数据

-(void)encodeWithCoder:(NSCoder *)coder;// 读取实例变量,并把这些数据写到coder中去。序列化数据

NSCoder 是一个抽象类,抽象类不能被实例话,只能提供一些想让子类继承的方法。

NSKeyedUnarchiver   从二进制流读取对象。

NSKeyedArchiver       把对象写到二进制流中去。

一般是在自己定义的类中需要在.h 文件中加入<NScoding>

在.m 文件众实现他的的两个代理方法,这个代理方法将会被自动调用

例如对一个数据类的封装如下:

@interface Restaurant : NSObject {
    NSString* shopID;       
    NSString* title;     
    NSString* discount; 
    NSString* address;   
}

@property (nonatomic, copy) NSString *shopID;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *discount;
@property (nonatomic, copy) NSString *address;
@end

@implementation Restaurant
@synthesize shopID,title,discount,address;
-(void)dealloc
{
    [shopID release];
    [title release];
    [discount release];
    [address release];

    [super dealloc];   
}

- (id)initWithCoder:(NSCoder *)coder
{
    if (self = [super init]) {
        shopID = [[coder decodeObjectForKey:@"shopid"] retain];
        title  = [[coder decodeObjectForKey:@"title"] retain];
        discount=[[coder decodeObjectForKey:@"discount"] retain];
        address=[[coder decodeObjectForKey:@"address"] retain];   
    }
    return self;
}

-(void)encodeWithCoder:(NSCoder *)coder
{
    [coder encodeObject:shopID forKey:@"shopid"];
    [coder encodeObject:title forKey:@"title"];
    [coder encodeObject:discount forKey:@"discount"];
    [coder encodeObject:address forKey:@"address"]; 
}
@end

下一步就是保存和加载数据了:

注:history对象是一个NSMutableArray数组,用来装Restaurant对象。

-(void) saveArchiver     //保存
{

    //获取路径和保存文件

    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* filename = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"history.dat"];
   
    if (history && [history count]>0) {
        [NSKeyedArchiver archiveRootObject:history toFile:filename];
    }else {
        //删除归档文件
        NSFileManager *defaultManager = [NSFileManager defaultManager];
        if ([defaultManager isDeletableFileAtPath:filename]) {
            [defaultManager removeItemAtPath:filename error:nil];
        }
    }
}

-(void) loadArchiver      //加载
{
    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* filename = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"history.dat"];
    self.history= [NSKeyedUnarchiver unarchiveObjectWithFile:filename];
}

你可能感兴趣的:(iOS 归档的使用)