实现NSCoding协议

1.例子:

AddressCard.h接口文件

#import<Foundation/Foundation.h>

@interface AddressCard:NSObject<NSCoding,NSCoping>

@property (copy,nonatomic) NSString *name,email;

-(void)setName:(NSString) *theName andEmail:(NSString *)theEmail;

-(NSComparisonResult) compareNames:(id)element;

-(void)print;

AddressCard.m文件

-(void)encodeWithCoder :(NSCoder *)decoder

{

   [encoder encodeObject:name forKey:@"AddressCardName"];

   [encoder encodeObject:email forKey:@"AddressCardEmail"];

//  [encoder encodeBool:YES forKey:@"BoolValue"];

}

-(void)initWithCoder :(NSCoder *)decoder

{

  name = [decoder decodeObjectforKey:@"AddressCardName"];

  email = [decoder decodeObjectforKey:@"AddressCardEmail"];

// boolValue = [decoder decodeBoolforKey:@"BoolValue"];

  return self;

}

使用:

if(NSKeyedArchiver archiveRootObject:myBook toFile:@"addrbook.arch"] == NO)

  NSLog(@"archiving failed");

 

AddressBook *mybook = [NSKeyedUnarchiver unarchiveObjectWithFile:@"addrbook.arch"];

 

2.将两个对象(均实现NSCoding协议)归档到同一文件

#import "AddressBook.h"

#import "Foo.h"

int main(int argc, char *argv[])

{

  @autoreleasepool{

    Foo *myFool1 = [Foo alloc]init];

    NSMutableData *dataArea;

    NSKeyedArchiver *archiver;

    AddressBook *myBook;

    //该处省略了为myFool1和myBook赋值

    

    //设置数据区,并将其连接到一个NSKeyedArchiver对象

    dataArea = [NSMutableData data];

    archiver = [[NSKeyedArchiver alloc] initForWritingWithMutaleData:dataArea];

    //现在可以开始存档对象

    [archiver encodeObject:myBook forKey:@"myaddrbook"];

    [archiver encodeObject:myFool1 forKey:@"myfool1"];

    [archiver finishEncoding];

 

    //将存档的数据写入文件

    if([dataArea writeToFile:@"myArchive" atomically :YES] == NO)

      NSLog(@"Archiving failed!");

}

return 0;

}

 解析

#import "AddressBook.h"

#import"Foo.h"

 

int main(int argc, char *argv[])

{

  @autoreleasepool{

    NSData *dataArea;

    NSKeyedUnarchiver *unarchiver;

    Foo *myFoo1;

    AddressBook *myBook;

    //从归档文件读取并连接

    //NSkeyedUnarchiver 对象

    dataArea = [NSData dataWithContentsOfFile:@"myArchive"];

    if(!dataArea){

      NSLog(@"Can't read back archive file!");

      return 1;

    } 

    unarchiver = [[NSKeyedUnarchiver alloc]initForReadingData:dataArea];

    //解码以前储存在归档文件中的对象

    myBook = [unarchiver decodeObjectForKey:@"myaddrbook"];

    myFoo1 = [unarchiver decodeObjectForKey:@"myfoo1"];

    [unarchiver finishDecoding];

 

    //验证是否还原成功

    [myBook list];

    NSLog(@%@\n%i\n%g),[myFoo1 strVal],[myFoo1 intVal],[myFoo1 floatVal]);

  }

  return 0;

 }

 

3.对与实现了NSCoding协议的对象,可以使用归档深复制对象:

NSMutableArray *dataArray = [NSMutableArray arrayWithObjects:[NSMutableString stringWithString:@"one"],[NSMutableString stringWithString:@"two"],[NSMutableString stringWithString:@"thress"],nil];

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dataArray];

NSMutableArray *dataArray2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];

 

NSMutableString *mStr = [dataArray2 objectAtIndex:0];

[mStr appendString@"ONE"];

输出

dataArray:

one

two

three

dataArray2:

oneONE

two

three

posted @ 2013-01-31 16:36  gagag  阅读(3254)  评论(0编辑  收藏  举报