一.概念解释:
归档是一种很常用的文件储存方法,几乎任何类型的对象都能够被归档储存(实际上是一种文件保存的形式),使用NSKeyedArichiver进行归档、NSKeyedUnarchiver进行接档,这种方式会在写入、读出数据之前对数据进行序列化、反序列化操作。可以对字符串、数字等进行归档,当然也可以对NSArray与NSDictionary进行归档。返回值Flag标志着是否归档成功,YES为成功,NO为失败。
二.单一归档/解挡:
- NSString *homeDictionary = NSHomeDirectory();//获取根目录
- NSString *homePath = [homeDictionary stringByAppendingPathComponent:@"atany.archiver"];//添加储存的文件名
- BOOL flag = [NSKeyedArchiver archiveRootObject:@”归档” toFile:homePath];//归档一个字符串
- [NSKeyedUnarchiver unarchiveObjectWithFile:homePath]
三.多个对象的归档/解档:
- //准备数据
- 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];
- 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);
- 首先我们需要,自定义一个实体类,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
- @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
- //保存图片与归档
- - (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];
- }
- 接档:
- - (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);
- }
备注: 要将一个自定义的类进行归档,那么类里面的每个属性都必须是可以被归档的,如果是不能归档的类型,我们可以把他转化为NSValue进行归档,然后在读出来的时候在转化为相应的类。
内容地址:http://www.cnblogs.com/ios8/p/ios-archive.html