iOS 歸檔的記錄ui
歸檔是一種很經常使用的文件儲存方法,幾乎任何類型的對象都可以被歸檔儲存(其實是一種文件保存的形式),瀏覽網上的一些資料後,並結合本身的一些經驗,總結成此文。編碼
使用NSKeyedArichiver進行歸檔、NSKeyedUnarchiver進行接檔,這種方式會在寫入、讀出數據以前對數據進行序列化、反序列化操做。atom
歸檔:spa
[cpp] view plaincopy.net
NSString *homeDictionary = NSHomeDirectory();//獲取根目錄 code
NSString *homePath = [homeDictionary stringByAppendingPathComponent:@"atany.archiver"];//添加儲存的文件名 對象
BOOL flag = [NSKeyedArchiver archiveRootObject:@」歸檔」 toFile:homePath];//歸檔一個字符串 blog
這種方式能夠對字符串、數字等進行歸檔,固然也能夠對NSArray與NSDictionary進行歸檔。返回值Flag標誌着是否歸檔成功,YES爲成功,NO爲失敗。圖片
接檔:字符串
[cpp] view plaincopy
[NSKeyedUnarchiver unarchiveObjectWithFile:homePath]
使用NSKeyedUnarchiver進行接檔(反序列化)。
這種歸檔的方式存在一個缺點:只能把一個對象歸檔進一個文件中,那麼怎麼對多個對象進行歸檔呢?
一樣是使用NSKeyedArchiver進行歸檔,不一樣的是同時歸檔多個對象,這裏咱們舉例放入了一個CGPoint點、字符串、整數(固然不少類型均可以的,例如UIImage、float等等),使用encodeXXX方法進行歸檔,最後經過writeToFile方法寫入文件。
歸檔:寫入數據
[cpp] view plaincopy
//準備數據
CGPoint point = CGPointMake(1.0, 2.0);
NSString *info = @"座標原點";
NSInteger value = 10;
NSString *multiHomePath = [NSHomeDirectory() stringByAppendingPathComponent:@"multi.archiver"];
NSMutableData *data = [[NSMutableData alloc]init];
NSKeyedArchiver *archvier = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
//對多個對象進行歸檔
[archvier encodeCGPoint:point forKey:@"kPoint"];
[archvier encodeObject:info forKey:@"kInfo"];
[archvier encodeInteger:value forKey:@"kValue"];
[archvier finishEncoding];
[data writeToFile:multiHomePath atomically:YES];
接檔:從路徑中得到數據構造NSKeyedUnarchiver實例,使用decodeXXXForKey方法得到文件中的對象。
[cpp] view plaincopy
NSMutableData *dataR = [[NSMutableData alloc]initWithContentsOfFile:multiHomePath];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:dateR];
CGPoint pointR = [unarchiver decodeCGPointForKey:@"kPoint"];
NSString *infoR = [unarchiver decodeObjectForKey:@"kInfo"];
NSInteger valueR = [unarchiver decodeIntegerForKey:@"kValue"];
[unarchiver finishDecoding];
NSLog(@"%f,%f,%@,%d",pointR.x,pointR.y,infoR,valueR);
能夠看出對多個對象進行歸檔仍是挺方便的,這裏又出現一個問題,這裏的對象都是基本類型數據,那麼怎麼對本身定義類生成的實例對象進行歸檔呢?
自定義對象,應用範圍很廣,由於它對應着MVC中的Model層,即實體類。在程序中,咱們會在Model層定義不少的entity,例如User,Teacher。。
那麼對自定義對象的歸檔顯得重要的多,由於不少狀況下咱們須要在Home鍵以後保存數據,在程序恢復時從新加載,那麼,歸檔即是一個好的選擇。
首先咱們須要,自定義一個實體類,Archive。
Archive.h
[cpp] view plaincopy
#import <Foundation/Foundation.h>
@interface Archive : NSObject
@property (copy,nonatomic) NSString *name;
@property NSInteger age;
@property (copy,nonatomic) NSString *address;
@property (copy,nonatomic) UIImage *photo;
@end
Archive.m
[cpp] view plaincopy
#import "Archive.h"
#define kNameKey @"NameKey"
#define kAgeKey @"AgeKey"
#define kAddress @"AddressKey"
#define kPhotoKey @"PhotoKey"
@implementation Archive
@synthesize name = _name;
@synthesize age = _age;
@synthesize address = _address;
@synthesize photo = _photo;
#pragma mark - NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:_name forKey:kNameKey];
[aCoder encodeInteger:_age forKey:kAgeKey];
[aCoder encodeObject:_address forKey:kAddress];
[aCoder encodeObject:_photo forKey:kPhotoKey];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
_name = [aDecoder decodeObjectForKey:kNameKey];
_age = [aDecoder decodeIntegerForKey:kAgeKey];
_address = [aDecoder decodeObjectForKey:kAddress];
_photo = [aDecoder decodeObjectForKey:kPhotoKey];
}
return self;
}
#pragma mark - NSCoping
- (id)copyWithZone:(NSZone *)zone {
Archive *copy = [[[self class] allocWithZone:zone] init];
copy.name = [self.name copyWithZone:zone];
copy.age = self.age;
copy.address = [self.address copyWithZone:zone];
copy.photo = self.photo;
return copy;
}
@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
//保存圖片與歸檔
- (IBAction)save:(id)sender {
//準備數據
NSString *name = @"小楊在玩iOS";
NSInteger age = 22;
NSString *address = @"你猜我在哪~";
UIImage *photo = [UIImage imageNamed:@"loginman.jpg"];
//存儲數據到類
Archive *archivingData = [[Archive alloc] init];
archivingData.name = name;
archivingData.age = age;
archivingData.address = address;
archivingData.photo = photo;
//歸檔
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:archivingData forKey:kArchivingDataKey]; // archivingDate的encodeWithCoder
方法被調用
[archiver finishEncoding];
//寫入文件
[data writeToFile:self.archivingFilePath atomically:YES];
}
接檔:
[cpp] view plaincopy
- (IBAction)loadArchive:(id)sender {
NSData *data = [[NSMutableData alloc] initWithContentsOfFile:self.archivingFilePath];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
//得到類
Archive *archivingData = [unarchiver decodeObjectForKey:kArchivingDataKey];// initWithCoder方法被調用
[unarchiver finishDecoding];
//讀取的數據
NSString *name = archivingData.name;
NSInteger age = archivingData.age;
NSString *address = archivingData.address;
self.imageView.image = archivingData.photo;
NSLog(@"%@||%d||%@",name,age,address);
}
咱們save以後loadArchive一次
打出結果爲:
2013-11-04 19:29:41.743TestArchives[16708:c07]小楊在玩iOS||22||你猜我在哪~
獲取成功。