NSKeyedArchiver 归档
给单一对象归档
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//新建一个数组a
NSArray *a = @[@"one",@"two",@"three",@"four",@"five"];
//在Documents文件夹下创建一个2.mp3的文件,存储路径为path
NSString *path = [NSString stringWithFormat:@"%@/Documents/2.mp3",NSHomeDirectory()];
//将数组a通过路径归档到文件2.mp3中
[NSKeyedArchiver archiveRootObject:a toFile:path];
//解归档
//通过路径path来解归档,因为归档前是一个数组对象,所以解归档后也用一个数组对象来接受
NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"%@", arr);
}
给多个对象归档
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//data归档
//在Documents中添加一个3.mp3的文件,将路径存储为pathData
NSString *pathData = [NSString stringWithFormat:@"%@/Documents/3.mp3", NSHomeDirectory()];
//新建一个Data
NSMutableData *dataNow = [[NSMutableData alloc]init];
//将Data归档到keyarch中
NSKeyedArchiver *keyarch = [[NSKeyedArchiver alloc]initForWritingWithMutableData:dataNow];
//往已经被归档的Data中添加数据
[keyarch encodeObject:@"haha" forKey:@"one"];
[keyarch encodeInteger:13 forKey:@"two"];
[keyarch encodeDouble:3.141592653 forKey:@"three"];
//!!!很重要,不要忘记!!!
[keyarch finishEncoding];
//通过路径pathData
[dataNow writeToFile:pathData atomically:YES];
//给多个对象解归档
//新建一个Data,通过路径pathData
NSData *dataOut = [NSData dataWithContentsOfFile:pathData];
//取出归档后的文件,存到unarch中
NSKeyedUnarchiver *unarch = [[NSKeyedUnarchiver alloc]initForReadingWithData:dataOut];
//将unarch中的值根据相对应的key,用相对应的类型去接受它
NSString *stringOut = [unarch decodeObjectForKey:@"one"];
NSInteger intOut = [unarch decodeIntegerForKey:@"two"];
double doubleOut = [unarch decodeDoubleForKey:@"three"];
//打印解归档后取出的值
NSLog(@"%@ %ld %f", stringOut,(long)intOut,doubleOut);
}
使用类来归档
先创建一个类people
.h文件
#import <Foundation/Foundation.h>
@interface people : NSObject
@property (nonatomic,strong)NSString *name;
@property (nonatomic,strong)NSString *age;
@end
.m文件
#import "people.h"
@implementation people
//归档函数,把调用者的实例(属性)进行归档
-(void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeObject:_age forKey:@"age"];
}
//解档函数,把调用者的实例(属性)进行解档
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeObjectForKey:@"age"];
}
return self;
}
//description函数,打印对象时可以写这个函数,可以看见(不会是乱码)
-(NSString *)description{
return [NSString stringWithFormat:@"%@ %@", self.name,self.age];
}
@end
调用函数的.m文件
#import "ViewController.h"
//先引入people.h头文件
#import "people.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化对象,并实例出对象
people *peopleOne = [[people alloc]init];
peopleOne.name = @"xiaoliu";
peopleOne.age = @"315";
//定义归档文件路径,自动调用归档函数
NSString *pathPeople = [NSString stringWithFormat:@"%@/Documents/5.mp3",NSHomeDirectory()];
//定义解档文件路径,自动调用解档函数
[NSKeyedArchiver archiveRootObject:peopleOne toFile:pathPeople];
//新建一个实例,来接受解归档的数据
people *peopleOut = [NSKeyedUnarchiver unarchiveObjectWithFile:pathPeople];
//打印解档后的实例
NSLog(@"%@", peopleOut.name);
}