在 Objective-C 语言中,归档是一个过程,即用某种格式来保存一个或多个对象,以便以后还原这些对象.
NSKeyedArchiver 进行归档(归档的数据为:二进制数据), NSKeyedUnarchiver进行解档
- NSString NSArray NSDictionary NSSet NSDate NSNumber NSData 可以直接归档
// /Users/wanglei/Library/Caches
NSString *cacheDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
// /Users/wanglei/Library/Caches/archive.plist
NSString *archivePath = [cacheDirectory stringByAppendingPathComponent:@"archive.plist"];
NSArray *infoArr = @[@{@"name":@"Roy",@"age":@"18",@"gender":@"man"},@{@"name":@"lily"}];
- 归档--序列化
[NSKeyedArchiver archiveRootObject:infoArr toFile:archivePath];
- 解档---反序列化
NSArray *info = [NSKeyedUnarchiver unarchiveObjectWithFile:archivePath];
// 有多种类型进行归档
NSString* path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];//缓存路径
NSString* archivePath = [path stringByAppendingPathComponent:@"archive.plist"];
NSLog(@"%@",archivePath);
//归档
NSMutableData* data = [[NSMutableData alloc]init];
NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
//对多个对象归档
[archiver encodeBool:YES forKey:@"isOr"];
[archiver encodeFloat:12.356 forKey:@"floatwe"];
[archiver finishEncoding];
[data writeToFile:archivePath atomically:YES];
//解档
NSMutableData* data1 = [[NSMutableData alloc]initWithContentsOfFile:archivePath];
NSKeyedUnarchiver* unarchive = [[NSKeyedUnarchiver alloc]initForReadingWithData:data1];
BOOL iOS = [unarchive decodeBoolForKey:@"isOr"];
float floatwe = [unarchive decodeFloatForKey:@"floatwe"];
[unarchive finishDecoding];
- 对自定义对象进行归档
#import <Foundation/Foundation.h>
@interface Worker : NSObject<NSCoding>
@property(nonatomic,copy)NSString *name;//姓名
@property(nonatomic,copy)NSString *age;//年龄
@property(nonatomic,copy)NSString *workType;//职业
@property(nonatomic,copy)NSString *workTime;//工作年限
@property(nonatomic,strong)NSDictionary *dict;
-(instancetype)initWithDictionary:(NSDictionary *)dict;
@end
#import "Worker.h"
@implementation Worker
-(instancetype)initWithDictionary:(NSDictionary *)dict
{
self = [super init];
if (self) {
_name = [dict objectForKey:@"name"];
_age = [dict objectForKey:@"age"];
_workType = [dict objectForKey:@"workType"];
_workTime = [dict objectForKey:@"workTime"];
}
return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeObject:_age forKey:@"age"];
[aCoder encodeObject:_workType forKey:@"workType"];
[aCoder encodeObject:_workTime forKey:@"workTime"];
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
_name = [aDecoder decodeObjectForKey:@"name"];
_age = [aDecoder decodeObjectForKey:@"age"];
_workType = [aDecoder decodeObjectForKey:@"workType"];
_workTime = [aDecoder decodeObjectForKey:@"workTime"];
}
return self;
}
@end
NSDictionary* dict = @{@"name":@"zxc",@"age":@"5",@"workType":@"iOS",@"workTime":@"2"};
Worker *worker = [[Worker alloc] initWithDictionary:dict];
NSLog(@"%@ %@ %@ %@",worker.age,worker.name,worker.workTime,worker.workType);
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
NSString *archivePath = [path stringByAppendingPathComponent:@"worker.plist"];
NSLog(@"%@",archivePath);
if ([NSKeyedArchiver archiveRootObject:worker toFile:archivePath]) {
Worker* worker = [NSKeyedUnarchiver unarchiveObjectWithFile:archivePath];
NSLog(@"%@ %@ %@ %@",worker.age,worker.name,worker.workTime,worker.workType);
}
网友评论