自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解Objective-C歸檔問題解決

移動開發(fā) iOS
Objcetive-C歸檔問題解決是本文要將誒少的內(nèi)容,主要是來學(xué)習(xí)在Objcetive-C如何來歸檔,本文很詳細的解決了這一問題,來看詳細內(nèi)容。

Objective-C歸檔問題解決是本文要將誒少的內(nèi)容,主要是來學(xué)習(xí)在Objective-C如何來歸檔,本文很詳細的解決了這一問題,來看詳細內(nèi)容。

對于基本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,需要實現(xiàn)NSCoding協(xié)議,然后用上述方法二歸檔:

代碼 

  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.    //深復(fù)制   
  42.    NSMutableString *tmpStr = [interest mutableCopy];   
  43.      newObj.interest = tmpStr;   
  44.   [tmpStr release];   
  45.   //淺復(fù)制   
  46.    //newObj.interest = interest;   
  47.    return newObj;   
  48.  }   
  49.  - (void)encodeWithCoder:(NSCoder *)aCoder{   
  50.    //如果是子類,應(yīng)該加上:   
  51.    //[super encodeWithCoder:aCoder];   
  52.   //注意這里如何處理對象的(其實是實現(xiàn)了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.    //如果是子類,應(yīng)該加上:   
  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創(chuàng)建定義檔案。

以上面已實現(xiàn)NSCoding協(xié)議的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]; 

利用歸檔實現(xiàn)對象深復(fù)制:

代碼 

  1. //先刪除TestProperty類中實現(xiàn)的NSCopying協(xié)議代碼。   
  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進行深復(fù)制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 

小結(jié):詳解Objective-C歸檔問題解決的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-07-29 16:16:30

Objective-c block

2011-08-17 10:58:59

Objective-C構(gòu)造函數(shù)

2013-08-21 14:57:42

objective-c問題

2011-07-18 16:36:51

Objective-C XCode

2015-10-08 10:01:10

Objective-CLayout

2014-04-01 10:50:42

iOS開發(fā)runtimeObjective-C

2011-08-17 10:29:39

Objective-C預(yù)處理

2011-08-04 13:38:01

Objective-C C++

2011-08-15 14:32:42

Objective-C委托協(xié)議

2011-08-17 11:05:22

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 函數(shù)

2011-08-16 10:23:04

Objective-CNSAutoreleaXcode常用鍵

2011-08-16 13:43:40

Objective-C文件cocoa

2011-07-08 18:44:09

Objective-C Self Super

2015-06-08 10:02:40

swiftOC兼容

2011-07-29 15:47:21

iPhone開發(fā) Objective- C

2011-08-04 18:14:42

Objective-C 消息

2011-08-04 10:04:17

Objective-C 分類 協(xié)議
點贊
收藏

51CTO技術(shù)棧公眾號