iOS archive(歸檔)的總結

iOS 歸檔的記錄ui

歸檔是一種很經常使用的文件儲存方法,幾乎任何類型的對象都可以被歸檔儲存(其實是一種文件保存的形式),瀏覽網上的一些資料後,並結合本身的一些經驗,總結成此文。編碼

1、使用archiveRootObject進行簡單的歸檔

使用NSKeyedArichiver進行歸檔、NSKeyedUnarchiver進行接檔,這種方式會在寫入、讀出數據以前對數據進行序列化、反序列化操做。atom

歸檔:spa

[cpp] view plaincopy.net

  1. NSString *homeDictionary = NSHomeDirectory();//獲取根目錄  code

  2. NSString *homePath  = [homeDictionary stringByAppendingPathComponent:@"atany.archiver"];//添加儲存的文件名  對象

  3. BOOL flag = [NSKeyedArchiver archiveRootObject:@」歸檔」 toFile:homePath];//歸檔一個字符串  blog

這種方式能夠對字符串、數字等進行歸檔,固然也能夠對NSArray與NSDictionary進行歸檔。返回值Flag標誌着是否歸檔成功,YES爲成功,NO爲失敗。圖片

接檔:字符串

[cpp] view plaincopy

  1. [NSKeyedUnarchiver unarchiveObjectWithFile:homePath]  


使用NSKeyedUnarchiver進行接檔(反序列化)。

這種歸檔的方式存在一個缺點:只能把一個對象歸檔進一個文件中,那麼怎麼對多個對象進行歸檔呢?


2、對多個對象的歸檔

一樣是使用NSKeyedArchiver進行歸檔,不一樣的是同時歸檔多個對象,這裏咱們舉例放入了一個CGPoint點、字符串、整數(固然不少類型均可以的,例如UIImage、float等等),使用encodeXXX方法進行歸檔,最後經過writeToFile方法寫入文件。

歸檔:寫入數據

[cpp] view plaincopy

  1. //準備數據  

  2. CGPoint point = CGPointMake(1.0, 2.0);  

  3. NSString *info = @"座標原點";  

  4. NSInteger value = 10;  

  5. NSString *multiHomePath = [NSHomeDirectory() stringByAppendingPathComponent:@"multi.archiver"];  

  6. NSMutableData *data = [[NSMutableData alloc]init];  

  7. NSKeyedArchiver *archvier = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];  

  8.   

  9. //對多個對象進行歸檔  

  10. [archvier encodeCGPoint:point forKey:@"kPoint"];  

  11. [archvier encodeObject:info forKey:@"kInfo"];  

  12. [archvier encodeInteger:value forKey:@"kValue"];  

  13. [archvier finishEncoding];  

  14. [data writeToFile:multiHomePath atomically:YES];  



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

[cpp] view plaincopy

  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、對自定義對象進行歸檔

自定義對象,應用範圍很廣,由於它對應着MVC中的Model層,即實體類。在程序中,咱們會在Model層定義不少的entity,例如User,Teacher。。

那麼對自定義對象的歸檔顯得重要的多,由於不少狀況下咱們須要在Home鍵以後保存數據,在程序恢復時從新加載,那麼,歸檔即是一個好的選擇。

首先咱們須要,自定義一個實體類,Archive。

Archive.h

[cpp] view plaincopy

  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

[cpp] view plaincopy

  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. @synthesize name = _name;  

  9. @synthesize age = _age;  

  10. @synthesize address = _address;  

  11. @synthesize photo = _photo;  

  12.   

  13. #pragma mark - NSCoding  

  14. - (void)encodeWithCoder:(NSCoder *)aCoder {  

  15.     [aCoder encodeObject:_name forKey:kNameKey];  

  16.     [aCoder encodeInteger:_age forKey:kAgeKey];  

  17.     [aCoder encodeObject:_address forKey:kAddress];  

  18.     [aCoder encodeObject:_photo forKey:kPhotoKey];  

  19. }  

  20.   

  21. - (id)initWithCoder:(NSCoder *)aDecoder {  

  22.     if (self = [super init]) {  

  23.         _name = [aDecoder decodeObjectForKey:kNameKey];  

  24.         _age = [aDecoder decodeIntegerForKey:kAgeKey];  

  25.         _address = [aDecoder decodeObjectForKey:kAddress];  

  26.         _photo = [aDecoder decodeObjectForKey:kPhotoKey];  

  27.     }  

  28.     return self;  

  29. }  

  30.   

  31. #pragma mark - NSCoping  

  32. - (id)copyWithZone:(NSZone *)zone {  

  33.     Archive *copy = [[[self class] allocWithZone:zone] init];  

  34.     copy.name = [self.name copyWithZone:zone];  

  35.     copy.age = self.age;  

  36.     copy.address = [self.address copyWithZone:zone];  

  37.     copy.photo = self.photo;  

  38.     return copy;  

  39. }  

  40. @end  



Archive類有四個字段(名字、年紀、地址、頭像),除了年紀爲整型以外,其餘的都看做Object。


【注】:要將一個自定義的類進行歸檔,那麼類裏面的每一個屬性都必須是能夠被歸檔的,若是是不能歸檔的類型,咱們能夠把他轉化爲NSValue進行歸檔,而後在讀出來的時候在轉化爲相應的類。


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的概念就是這樣,下面看看這個自定義類歸檔的具體代碼,其實和多個對象的歸檔是同樣的。。。

 

歸檔:

[cpp] view plaincopy

  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. }  


接檔:

[cpp] view plaincopy

  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. }  


咱們save以後loadArchive一次

打出結果爲:

2013-11-04 19:29:41.743TestArchives[16708:c07]小楊在玩iOS||22||你猜我在哪~

獲取成功。

相關文章
相關標籤/搜索