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

iPhone應(yīng)用開發(fā)之模型對(duì)象歸檔

移動(dòng)開發(fā) iOS
本文介紹的是iPhone應(yīng)用開發(fā)中的模型對(duì)象歸檔,主要是來介紹歸檔,它指的是另一種型式的序列化,但它是任保對(duì)象都可以實(shí)現(xiàn)的更常規(guī)的類型。其作用為:進(jìn)行數(shù)據(jù)的持久化保存。

iPhone應(yīng)用開發(fā)之模型對(duì)象歸檔是本文要介紹的內(nèi)容,主要是來介紹歸檔,它指的是另一種型式的序列化,但它是任保對(duì)象都可以實(shí)現(xiàn)的更常規(guī)的類型。其作用為:進(jìn)行數(shù)據(jù)的持久化保存。來看詳細(xì)內(nèi)容。

對(duì)象必須實(shí)現(xiàn)NSCodeing協(xié)議和NSCopying協(xié)議。

  1. @interface FourLines : NSObject <NSCoding, NSCopying> {  
  2.     NSString *field1;  
  3.     NSString *field2;  
  4.     NSString *field3;  
  5.     NSString *field4;     
  6. }  
  7.  
  8. @property (nonatomic, retain) NSString *field1;  
  9. @property (nonatomic, retain) NSString *field2;  
  10. @property (nonatomic, retain) NSString *field3;  
  11. @property (nonatomic, retain) NSString *field4;  
  12. @end  
  13. #pragma mark NSCoding  
  14. - (void)encodeWithCoder:(NSCoder *)encoder {  
  15.     [encoder encodeObject:field1 forKey:kField1Key];  
  16.     [encoder encodeObject:field2 forKey:kField2Key];  
  17.     [encoder encodeObject:field3 forKey:kField3Key];  
  18.     [encoder encodeObject:field4 forKey:kField4Key];  
  19. }  
  20. - (id)initWithCoder:(NSCoder *)decoder {  
  21.     if (self = [super init]) {  
  22.         self.field1 = [decoder decodeObjectForKey:kField1Key];  
  23.         self.field2 = [decoder decodeObjectForKey:kField2Key];  
  24.         self.field3 = [decoder decodeObjectForKey:kField3Key];  
  25.         self.field4 = [decoder decodeObjectForKey:kField4Key];  
  26.     }  
  27.     return self;  
  28. }  
  29. #pragma mark NSCopying  
  30. - (id)copyWithZone:(NSZone *)zone {  
  31.     FourLines *copy = [[[self class] allocWithZone: zone] init];  
  32.     copy.field1 = [[self.field1 copyWithZone:zone] autorelease];  
  33.     copy.field2 = [[self.field2 copyWithZone:zone] autorelease];  
  34.     copy.field3 = [[self.field3 copyWithZone:zone] autorelease];  
  35.     copy.field4 = [[self.field4 copyWithZone:zone] autorelease];  
  36.     return copy;  

獲取歸檔文件

  1. - (NSString *)dataFilePath {  
  2.     NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);  
  3.     NSString *documentsDirectory = [paths objectAtIndex:0];  
  4.     return [documentsDirectory stringByAppendingPathComponent:@"archive"];  

對(duì)數(shù)據(jù)進(jìn)行歸檔

  1. FourLines *fourLines = [[FourLines alloc] init];  
  2.     fourLines.field1 = field1.text;  
  3.     fourLines.field2 = field2.text;  
  4.     fourLines.field3 = field3.text;  
  5.     fourLines.field4 = field4.text;  
  6. //對(duì)數(shù)據(jù)進(jìn)行歸檔  
  7.     NSMutableData *data = [[NSMutableData alloc] init];  
  8.     NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];  
  9.     [archiver encodeObject:fourLines forKey:@"Data"];  
  10.     [archiver finishEncoding];  
  11.     [data writeToFile:[self dataFilePath] atomically:YES];  
  12.     [fourLines release];  
  13.     [archiver release];  
  14.     [data release];    

獲取歸檔數(shù)據(jù)

  1. NSString *filePath = [self dataFilePath];  
  2. if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {  
  3.     NSData *data = [[NSMutableData alloc]  
  4.                     initWithContentsOfFile:[self dataFilePath]];  
  5.     NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]  
  6.                                      initForReadingWithData:data];  
  7.     FourLines *fourLines = [unarchiver decodeObjectForKey:@"Data"];  
  8.     [unarchiver finishDecoding];  
  9.     field1.text = fourLines.field1;  
  10.     field2.text = fourLines.field2;  
  11.     field3.text = fourLines.field3;  
  12.     field4.text = fourLines.field4;  
  13.     [unarchiver release];  
  14.     [data release];         

小結(jié):iPhone應(yīng)用開發(fā)之模型對(duì)象歸檔的內(nèi)容介紹完了,希望通過本文對(duì)你有所幫助!

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

2011-08-10 10:10:21

iPhoneUIPopoverCo

2011-08-11 10:03:43

iPhonecocoaNSRunLoop

2011-08-17 10:24:50

iPhone開發(fā)UIImage圖片

2011-08-09 11:36:41

iPhoneUIPickerVieDEMO

2011-08-10 09:50:43

iPhoneArchive數(shù)據(jù)

2011-08-15 17:52:21

iPhone應(yīng)用對(duì)象NSString

2011-08-17 15:19:38

iPhone應(yīng)用數(shù)據(jù)

2011-08-03 16:01:24

iPhone應(yīng)用開發(fā) 自動(dòng)登陸

2011-07-29 10:41:27

IPhone 應(yīng)用開發(fā) 照相機(jī)

2011-08-01 18:27:58

iPhone開發(fā) UISearchBa

2011-08-08 16:56:44

iPhone 字符處理 視圖

2011-08-08 10:10:14

iPhone開發(fā) 圖片 方法

2011-08-09 17:29:29

iPhone文件屏幕

2011-07-19 09:46:38

2011-07-08 14:58:16

iPhone Xcode iOS

2011-07-19 09:58:36

2014-07-30 09:56:41

iPhoneiPad

2011-08-08 13:57:19

iPhone開發(fā) 打包 DEB

2011-08-09 13:10:32

iPhone地圖開發(fā)

2011-08-05 14:48:06

iPhone應(yīng)用 異步隊(duì)列
點(diǎn)贊
收藏

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