歸檔

一:什麼是歸檔ui

對象歸檔是將對象歸檔以文件的形式保存到磁盤中(也稱爲序列化,持久化)使用的時候讀取該文件的保存路徑讀取文件的內容(也稱爲接檔,反序列化)編碼

對象歸檔的文件是保密的磁盤上沒法查看文件中的內容,而屬性列表是明文的能夠查看atom

1,使用archiveRootObject進行簡單地歸檔和解檔(對一個對象進行歸檔)spa

- (void)viewDidLoad {code

    [super viewDidLoad];對象

    NSString *str = @"afa";圖片

    NSString *astr = @"111";string

    NSArray *Array = [NSArray arrayWithObjects:str, astr, nil];it

    //歸檔io

    NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *filename = [Path stringByAppendingPathComponent:@"test"];

    [NSKeyedArchiver archiveRootObject:Array toFile:filename];

    

    //解檔

    NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile: filename];

    str = [arr objectAtIndex:0];

    astr =  [arr objectAtIndex:1];

    NSLog(@"str:%@",str);

    NSLog(@"astr:%@",astr);

 }

2,對多個對象的歸檔(對基本類型數據)

歸檔(寫入數據)

 step1:準備數據

  1. CGPoint point = CGPointMake(1.0, 2.0);  
  2. NSString *info = @"座標原點";  
  3. NSInteger value = 10;  
  4. NSString *multiHomePath = [NSHomeDirectory() stringByAppendingPathComponent:@"multi.archiver"];  
  5. NSMutableData *data = [[NSMutableData alloc]init];  
  6. NSKeyedArchiver *archvier = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];  

step2:對多個對象進行歸檔  

  1. [archvier encodeCGPoint:point forKey:@"kPoint"];  
  2. [archvier encodeObject:info forKey:@"kInfo"];  
  3. [archvier encodeInteger:value forKey:@"kValue"];  
  4. [archvier finishEncoding];  
  5. [data writeToFile:multiHomePath atomically:YES]; 

解檔(路徑中得到數據構造NSKeyedUnarchiver實例,使用decodeXXXForKey方法得到文件中的對象。)

  1. NSMutableData *dataR = [[NSMutableData alloc]initWithContentsOfFile:multiHomePath];  
  2. NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:dateR];  
  3. CGPoint pointR = [unarchiver decodeCGPointForKey:@"kPoint"];  
  4. NSString *infoR = [unarchiver decodeObjectForKey:@"kInfo"];  
  5. NSInteger valueR = [unarchiver decodeIntegerForKey:@"kValue"];  
  6. [unarchiver finishDecoding];  
  7. NSLog(@"%f,%f,%@,%d",pointR.x,pointR.y,infoR,valueR); 

3,對自定義對象進行歸檔

step1:自定義一個實體類Archive

Archive.h

  1. #import <Foundation/Foundation.h>  
  2.  
  3. @interface Archive : NSObject  
  4. @property (copy,nonatomic) NSString *name;  
  5. @property NSInteger age;  
  6. @property (copy,nonatomic) NSString *address;  
  7. @property (copy,nonatomic) UIImage *photo;  
  8.   
  9. @end 

Archive.m

  1. #import "Archive.h"  
  2. #define kNameKey @"NameKey"  
  3. #define kAgeKey @"AgeKey"  
  4. #define kAddress @"AddressKey"  
  5. #define kPhotoKey @"PhotoKey"  
  6.   
  7. @implementation Archive  
  8.   
  9. #pragma mark - NSCoding  
  10. - (void)encodeWithCoder:(NSCoder *)aCoder {  
  11.     [aCoder encodeObject:_name forKey:kNameKey];  
  12.     [aCoder encodeInteger:_age forKey:kAgeKey];  
  13.     [aCoder encodeObject:_address forKey:kAddress];  
  14.     [aCoder encodeObject:_photo forKey:kPhotoKey];  
  15. }  
  16.   
  17. - (id)initWithCoder:(NSCoder *)aDecoder {  
  18.     if (self = [super init]) {  
  19.         _name = [aDecoder decodeObjectForKey:kNameKey];  
  20.         _age = [aDecoder decodeIntegerForKey:kAgeKey];  
  21.         _address = [aDecoder decodeObjectForKey:kAddress];  
  22.         _photo = [aDecoder decodeObjectForKey:kPhotoKey];  
  23.     }  
  24.     return self;  
  25. }  
  26.   
  27. #pragma mark - NSCoping  
  28. - (id)copyWithZone:(NSZone *)zone {  
  29.     Archive *copy = [[[self class] allocWithZone:zone] init];  
  30.     copy.name = [self.name copyWithZone:zone];  
  31.     copy.age = self.age;  
  32.     copy.address = [self.address copyWithZone:zone];  
  33.     copy.photo = self.photo;  
  34.     return copy;  
  35. }  
  36. @end  

 

Archive實現了三個委託方法1)encodeWithCoder: 2)initWithCoder:  3)copyWithZone:

1)encodeWithCoder

Encodes the receiverusing a given archiver

經過一個給定的archiver把消息接收者進行編碼。

當接收到encodeObject消息的時候,類終端encodeWithCoder方法被調用。

2)initWithCoder

Returns an objectinitialized from data in a given unarchiver. (required)

從一個給定unarchiver的數據中返回一個初始化對象。

3)copyWithZone

Returnsa new instance that’s a copy of the receiver

返回消息接收者的一個複製的新實例。

SDK的概念就是這樣,下面看看這個自定義類歸檔的具體代碼,其實和多個對象的歸檔是同樣的。。。

 

step2:歸檔:

  1. //保存圖片與歸檔  
  2. - (IBAction)save:(id)sender {  
  3.       
  4.     //準備數據  
  5.     NSString *name = @"小楊在玩iOS";  
  6.     NSInteger age = 22;  
  7.     NSString *address = @"你猜我在哪~";  
  8.     UIImage *photo = [UIImage imageNamed:@"loginman.jpg"];  
  9.     //存儲數據到類  
  10.     Archive *archivingData = [[Archive alloc] init];  
  11.     archivingData.name = name;  
  12.     archivingData.age = age;  
  13.     archivingData.address = address;  
  14.     archivingData.photo = photo;  
  15.       
  16.     //歸檔  
  17.     NSMutableData *data = [[NSMutableData alloc] init];  
  18.     NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];  
  19.   
  20.     [archiver encodeObject:archivingData forKey:kArchivingDataKey]; // archivingDate的encodeWithCoder  
  21. 方法被調用  
  22.     [archiver finishEncoding];  
  23.     //寫入文件  
  24.     [data writeToFile:self.archivingFilePath atomically:YES];  
  25. }  

 

step3:接檔:

  1. - (IBAction)loadArchive:(id)sender {  
  2.     NSData *data = [[NSMutableData alloc] initWithContentsOfFile:self.archivingFilePath];  
  3.     NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];  
  4.       
  5.     //得到類  
  6.     Archive *archivingData = [unarchiver decodeObjectForKey:kArchivingDataKey];// initWithCoder方法被調用  
  7.     [unarchiver finishDecoding];  
  8.       
  9.     //讀取的數據  
  10.     NSString *name = archivingData.name;  
  11.     NSInteger age = archivingData.age;  
  12.     NSString *address = archivingData.address;  
  13.     self.imageView.image = archivingData.photo;  
  14.     NSLog(@"%@||%d||%@",name,age,address);  
  15. }  
相關文章
相關標籤/搜索