詳解Objective-C歸檔問題解決

來源:互聯網
上載者:User

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歸檔問題解決的內容介紹完了,希望通過本文的學習能對你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.