详解Objective-C归档问题解决

移动开发 iOS
Objcetive-C归档问题解决是本文要将诶少的内容,主要是来学习在Objcetive-C如何来归档,本文很详细的解决了这一问题,来看详细内容。

Objective-C归档问题解决是本文要将诶少的内容,主要是来学习在Objective-C如何来归档,本文很详细的解决了这一问题,来看详细内容。

对于基本Objective-C类对象(NSString,NSArray...):

方法一:使用XML属性列表进行归档。

代码

  1.  NSDictionary *glossay;   
  2. //存   
  3.   glossay = [NSDictionary dictionaryWithObjectsAndKeys:@"obj val 1",@"key1",@"obj val 2",@"key2",nil];   
  4.  if ([glossay writeToFile:@"glossary" atomically:YES] == NO) {   
  5.    NSLog(@"Save to file failed!");   
  6. }   
  7. //取   
  8.  glossay = [NSDictionary dictionaryWithContentsOfFile:@"glossary"];  
  9. NSLog(@"%@",[glossay valueForKey:@"key2"]); 

方法二:使用NSKeyedArchiver归档。

代码

  1.  NSDictionary *glossay;   
  2. glossay = [NSDictionary dictionaryWithObjectsAndKeys:@"obj val 1",@"key1",@"obj val 2",@"key2",nil];   
  3.  //存   
  4.  if ([NSKeyedArchiver archiveRootObject:glossay toFile:@"glossay.archiver"] == NO) {   
  5.    NSLog(@"write file fail!!");   
  6. }   
  7. //取  
  8.  glossay = [NSKeyedUnarchiver unarchiveObjectWithFile:@"glossay.archiver"];  
  9. NSLog(@"%@",[glossay valueForKey:@"key2"]); 

对于自定义的Class,需要实现NSCoding协议,然后用上述方法二归档:

代码 

  1.  //TestProperty.h    
  2.   #import <Cocoa/Cocoa.h>    
  3.  @interface TestProperty : NSObject <NSCopying,NSCoding>{    
  4.   NSString *name;    
  5.   NSString *password;    
  6.    NSMutableString *interest;    
  7.   NSInteger myInt;   
  8.  }  
  9.   12 @property (retain,nonatomic) NSString *name,*password;   
  10.  @property (retain,nonatomic) NSMutableString *interest;   
  11.  @property NSInteger myInt;   
  12.  -(void) rename:(NSString *)newname;   
  13.  @end   
  14.  ====================   
  15.  //TestProperty.m   
  16.   23 #import "TestProperty.h"   
  17.   25  26 @implementation TestProperty   
  18.   28 @synthesize name,password,interest;   
  19.  @synthesize myInt;   
  20.  -(void) rename:(NSString *)newname{   
  21.    // 这里可以直接写成   
  22.    // self.name = newname;   
  23.   //   
  24.     if (name != newname) {   
  25.      [name autorelease];   
  26.      name = newname;   
  27.      [name retain];   
  28.    }   
  29.  }   
  30.  -(void) dealloc{   
  31.    self.name = nil;   
  32. self.password = nil;   
  33.   self.interest = nil;   
  34.   [super dealloc];   
  35.  }   
  36.  - (id)copyWithZone:(NSZone *)zone{   
  37.    TestProperty *newObj = [[[self class] allocWithZone:zone] init];   
  38.    newObj.name = name;   
  39.    newObj.password = password;   
  40.     newObj.myInt = myInt;   
  41.    //深复制   
  42.    NSMutableString *tmpStr = [interest mutableCopy];   
  43.      newObj.interest = tmpStr;   
  44.   [tmpStr release];   
  45.   //浅复制   
  46.    //newObj.interest = interest;   
  47.    return newObj;   
  48.  }   
  49.  - (void)encodeWithCoder:(NSCoder *)aCoder{   
  50.    //如果是子类,应该加上:   
  51.    //[super encodeWithCoder:aCoder];   
  52.   //注意这里如何处理对象的(其实是实现了NSCoding的类)!   
  53.     [aCoder encodeObject:name forKey: @"TestPropertyName"];   
  54.       [aCoder encodeObject:password forKey:@"TestPropertyPassword"];   
  55.        [aCoder encodeObject:interest forKey:@"TestPropertyInterest"];   
  56.       //注意这里如何处理基本类型!   
  57.      [aCoder encodeInt:myInt forKey:@"TestPropertyMyInt"];   
  58.    }  
  59.  - (id)initWithCoder:(NSCoder *)aDecoder{   
  60.    //如果是子类,应该加上:   
  61.   //self = [super initWithCoder:aDecoder];   
  62.    //解码对象   
  63.     name = [[aDecoder decodeObjectForKey:@"TestPropertyName"] retain];   
  64.      password = [[aDecoder decodeObjectForKey:@"TestPropertyPassword"] retain];   
  65.     interest = [[aDecoder decodeObjectForKey:@"TestPropertyInterest"] retain];   
  66.    //解码基本类型   
  67.    myInt = [aDecoder decodeIntForKey:@"TestPropertyMyInt"];   
  68.      return self;   
  69. }   
  70.   @end   
  71.  ===============   
  72.   //测试  
  73. //存   
  74.    TestProperty *test = [[TestProperty alloc] init];   
  75.      test.name = @"pxl";  
  76.      test.password = @"pwd...";  
  77.     test.interest = [NSMutableString stringWithString:@"interest..."];  
  78.      test.myInt = 123;  
  79.        if([NSKeyedArchiver archiveRootObject:test toFile:@"testproerty.archive"] == NO){  
  80.         NSLog(@"write to file fail!!");  
  81.        }  
  82.       //取  
  83.     TestProperty *test = [NSKeyedUnarchiver unarchiveObjectWithFile:@"testproerty.archive"];  
  84.    NSLog(@"%@",test.name); 

使用NSData创建定义档案。

以上面已实现NSCoding协议的TestProperty类为例,代码

  1.  //存   
  2.     TestProperty *test = [[TestProperty alloc] init];   
  3.  test.name = @"pxl";   
  4. test.password = @"pwd...";   
  5.   test.interest = [NSMutableString stringWithString:@"interest..."];   
  6.   test.myInt = 123;   
  7.  NSMutableData *dataArea = [NSMutableData data];   
  8.   NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:dataArea];  
  9.   [archiver encodeObject:test forKey:@"testObj"];  
  10.   //这里还可以加其它的对象13   //......  
  11.  [archiver finishEncoding];  
  12.   if ([dataArea writeToFile:@"test.archiver" atomically:YES] == NO) {  
  13.     NSLog(@"write to file fail...");  
  14.   }  
  15.  [archiver release];  
  16. [test release];  
  17.  ============  
  18.     //取  
  19.    NSData *dataArea = [NSData dataWithContentsOfFile:@"test.archiver"];  
  20.  if(!dataArea){  
  21.     NSLog(@"Can't read back archive file");  
  22.     return (1);  
  23.   }  
  24.   NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:dataArea];    
  25. TestProperty *test = [unarchiver decodeObjectForKey:@"testObj"];  
  26.   [unarchiver finishDecoding];  
  27.   NSLog(@"%@",test.name);  
  28.   [unarchiver release]; 

利用归档实现对象深复制:

代码 

  1. //先删除TestProperty类中实现的NSCopying协议代码。   
  2.  TestProperty *test = [[TestProperty alloc] init];   
  3.  test.name = @"pxl";  
  4. est.password = @"pwd...";   
  5. test.interest = [NSMutableString stringWithString:@"interest..."];   
  6.  test.myInt = 123;   
  7. //对test进行深复制10    NSData *data =  [NSKeyedArchiver archivedDataWithRootObject:test];11   
  8. TestProperty *test2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];  
  9. [test2.interest appendString:@"film"];  
  10. NSLog(@"%@",test.interest);15   NSLog(@"%@",test2.interest);  
  11.  //输出  
  12.  2010-12-30 16:11:47.391 HelloWorld[4599:a0f] interest...  
  13.  2010-12-30 16:11:47.393 HelloWorld[4599:a0f] interest...film 

小结:详解Objective-C归档问题解决的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-07-29 16:16:30

Objective-c block

2011-08-17 10:58:59

Objective-C构造函数

2013-08-21 14:57:42

objective-c问题

2015-10-08 10:01:10

Objective-CLayout

2011-07-18 16:36:51

Objective-C XCode

2014-04-01 10:50:42

iOS开发runtimeObjective-C

2011-08-17 10:29:39

Objective-C预处理

2011-08-04 13:38:01

Objective-C C++

2011-08-15 14:32:42

Objective-C委托协议

2011-07-27 16:55:12

Objective-c 闭包

2014-04-28 09:56:56

Objective-CiOS命名空间

2011-08-01 17:11:43

Objective-C 函数

2011-08-17 11:05:22

Objective-C方法

2011-08-16 13:43:40

Objective-C文件cocoa

2011-07-08 18:44:09

Objective-C Self Super

2011-08-16 10:23:04

Objective-CNSAutoreleaXcode常用键

2015-06-08 10:02:40

swiftOC兼容

2011-07-29 15:47:21

iPhone开发 Objective- C

2011-08-04 18:14:42

Objective-C 消息

2011-07-27 16:36:03

iphone Objective- 静态库
点赞
收藏

51CTO技术栈公众号