一:什麼是歸檔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:準備數據
- 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];
step2:對多個對象進行歸檔
- [archvier encodeCGPoint:point forKey:@"kPoint"];
- [archvier encodeObject:info forKey:@"kInfo"];
- [archvier encodeInteger:value forKey:@"kValue"];
- [archvier finishEncoding];
- [data writeToFile:multiHomePath atomically:YES];
解檔(路徑中得到數據構造NSKeyedUnarchiver實例,使用decodeXXXForKey方法得到文件中的對象。)
- 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);
3,對自定義對象進行歸檔
step1:自定義一個實體類Archive
Archive.h
- #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
- #import "Archive.h"
- #define kNameKey @"NameKey"
- #define kAgeKey @"AgeKey"
- #define kAddress @"AddressKey"
- #define kPhotoKey @"PhotoKey"
-
- @implementation Archive
-
- #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實現了三個委託方法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:歸檔:
- - (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];
- 方法被調用
- [archiver finishEncoding];
-
- [data writeToFile:self.archivingFilePath atomically:YES];
- }
step3:接檔:
- - (IBAction)loadArchive:(id)sender {
- NSData *data = [[NSMutableData alloc] initWithContentsOfFile:self.archivingFilePath];
- NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
-
-
- Archive *archivingData = [unarchiver decodeObjectForKey:kArchivingDataKey];
- [unarchiver finishDecoding];
-
-
- NSString *name = archivingData.name;
- NSInteger age = archivingData.age;
- NSString *address = archivingData.address;
- self.imageView.image = archivingData.photo;
- NSLog(@"%@||%d||%@",name,age,address);
- }