詳解Objective-C歸檔問題解決
Objective-C歸檔問題解決是本文要將誒少的內(nèi)容,主要是來學(xué)習(xí)在Objective-C如何來歸檔,本文很詳細(xì)的解決了這一問題,來看詳細(xì)內(nèi)容。
對(duì)于基本Objective-C類對(duì)象(NSString,NSArray...):
方法一:使用XML屬性列表進(jìn)行歸檔。
代碼
- NSDictionary *glossay;
 - //存
 - glossay = [NSDictionary dictionaryWithObjectsAndKeys:@"obj val 1",@"key1",@"obj val 2",@"key2",nil];
 - if ([glossay writeToFile:@"glossary" atomically:YES] == NO) {
 - NSLog(@"Save to file failed!");
 - }
 - //取
 - glossay = [NSDictionary dictionaryWithContentsOfFile:@"glossary"];
 - NSLog(@"%@",[glossay valueForKey:@"key2"]);
 
方法二:使用NSKeyedArchiver歸檔。
代碼
- NSDictionary *glossay;
 - glossay = [NSDictionary dictionaryWithObjectsAndKeys:@"obj val 1",@"key1",@"obj val 2",@"key2",nil];
 - //存
 - if ([NSKeyedArchiver archiveRootObject:glossay toFile:@"glossay.archiver"] == NO) {
 - NSLog(@"write file fail!!");
 - }
 - //取
 - glossay = [NSKeyedUnarchiver unarchiveObjectWithFile:@"glossay.archiver"];
 - NSLog(@"%@",[glossay valueForKey:@"key2"]);
 
對(duì)于自定義的Class,需要實(shí)現(xiàn)NSCoding協(xié)議,然后用上述方法二歸檔:
代碼
- //TestProperty.h
 - #import <Cocoa/Cocoa.h>
 - @interface TestProperty : NSObject <NSCopying,NSCoding>{
 - NSString *name;
 - NSString *password;
 - NSMutableString *interest;
 - NSInteger myInt;
 - }
 - 12 @property (retain,nonatomic) NSString *name,*password;
 - @property (retain,nonatomic) NSMutableString *interest;
 - @property NSInteger myInt;
 - -(void) rename:(NSString *)newname;
 - @end
 - ====================
 - //TestProperty.m
 - 23 #import "TestProperty.h"
 - 25 26 @implementation TestProperty
 - 28 @synthesize name,password,interest;
 - @synthesize myInt;
 - -(void) rename:(NSString *)newname{
 - // 這里可以直接寫成
 - // self.name = newname;
 - //
 - if (name != newname) {
 - [name autorelease];
 - name = newname;
 - [name retain];
 - }
 - }
 - -(void) dealloc{
 - self.name = nil;
 - self.password = nil;
 - self.interest = nil;
 - [super dealloc];
 - }
 - - (id)copyWithZone:(NSZone *)zone{
 - TestProperty *newObj = [[[self class] allocWithZone:zone] init];
 - newObj.name = name;
 - newObj.password = password;
 - newObj.myInt = myInt;
 - //深復(fù)制
 - NSMutableString *tmpStr = [interest mutableCopy];
 - newObj.interest = tmpStr;
 - [tmpStr release];
 - //淺復(fù)制
 - //newObj.interest = interest;
 - return newObj;
 - }
 - - (void)encodeWithCoder:(NSCoder *)aCoder{
 - //如果是子類,應(yīng)該加上:
 - //[super encodeWithCoder:aCoder];
 - //注意這里如何處理對(duì)象的(其實(shí)是實(shí)現(xiàn)了NSCoding的類)!
 - [aCoder encodeObject:name forKey: @"TestPropertyName"];
 - [aCoder encodeObject:password forKey:@"TestPropertyPassword"];
 - [aCoder encodeObject:interest forKey:@"TestPropertyInterest"];
 - //注意這里如何處理基本類型!
 - [aCoder encodeInt:myInt forKey:@"TestPropertyMyInt"];
 - }
 - - (id)initWithCoder:(NSCoder *)aDecoder{
 - //如果是子類,應(yīng)該加上:
 - //self = [super initWithCoder:aDecoder];
 - //解碼對(duì)象
 - name = [[aDecoder decodeObjectForKey:@"TestPropertyName"] retain];
 - password = [[aDecoder decodeObjectForKey:@"TestPropertyPassword"] retain];
 - interest = [[aDecoder decodeObjectForKey:@"TestPropertyInterest"] retain];
 - //解碼基本類型
 - myInt = [aDecoder decodeIntForKey:@"TestPropertyMyInt"];
 - return self;
 - }
 - @end
 - ===============
 - //測(cè)試
 - //存
 - TestProperty *test = [[TestProperty alloc] init];
 - test.name = @"pxl";
 - test.password = @"pwd...";
 - test.interest = [NSMutableString stringWithString:@"interest..."];
 - test.myInt = 123;
 - if([NSKeyedArchiver archiveRootObject:test toFile:@"testproerty.archive"] == NO){
 - NSLog(@"write to file fail!!");
 - }
 - //取
 - TestProperty *test = [NSKeyedUnarchiver unarchiveObjectWithFile:@"testproerty.archive"];
 - NSLog(@"%@",test.name);
 
使用NSData創(chuàng)建定義檔案。
以上面已實(shí)現(xiàn)NSCoding協(xié)議的TestProperty類為例,代碼
- //存
 - TestProperty *test = [[TestProperty alloc] init];
 - test.name = @"pxl";
 - test.password = @"pwd...";
 - test.interest = [NSMutableString stringWithString:@"interest..."];
 - test.myInt = 123;
 - NSMutableData *dataArea = [NSMutableData data];
 - NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:dataArea];
 - [archiver encodeObject:test forKey:@"testObj"];
 - //這里還可以加其它的對(duì)象13 //......
 - [archiver finishEncoding];
 - if ([dataArea writeToFile:@"test.archiver" atomically:YES] == NO) {
 - NSLog(@"write to file fail...");
 - }
 - [archiver release];
 - [test release];
 - ============
 - //取
 - NSData *dataArea = [NSData dataWithContentsOfFile:@"test.archiver"];
 - if(!dataArea){
 - NSLog(@"Can't read back archive file");
 - return (1);
 - }
 - NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:dataArea];
 - TestProperty *test = [unarchiver decodeObjectForKey:@"testObj"];
 - [unarchiver finishDecoding];
 - NSLog(@"%@",test.name);
 - [unarchiver release];
 
利用歸檔實(shí)現(xiàn)對(duì)象深復(fù)制:
代碼
- //先刪除TestProperty類中實(shí)現(xiàn)的NSCopying協(xié)議代碼。
 - TestProperty *test = [[TestProperty alloc] init];
 - test.name = @"pxl";
 - est.password = @"pwd...";
 - test.interest = [NSMutableString stringWithString:@"interest..."];
 - test.myInt = 123;
 - //對(duì)test進(jìn)行深復(fù)制10 NSData *data = [NSKeyedArchiver archivedDataWithRootObject:test];11
 - TestProperty *test2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
 - [test2.interest appendString:@"film"];
 - NSLog(@"%@",test.interest);15 NSLog(@"%@",test2.interest);
 - //輸出
 - 2010-12-30 16:11:47.391 HelloWorld[4599:a0f] interest...
 - 2010-12-30 16:11:47.393 HelloWorld[4599:a0f] interest...film
 
小結(jié):詳解Objective-C歸檔問題解決的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對(duì)你有所幫助!















 
 
 

 
 
 
 