归档:将对象类型转换成数据类型,以文件形式存储,如果想读取归档中的数据,可以通过反归档形式,还原数据
归档三种形式
1.将Foundation框架中对象进行归档
2.对自定义内容进行归档
3.自定义类对象进行归档
一、新建类Person
/*
1.首先:复杂对象所属的类必须遵循<NSCoding>
- (void)encodeWithCoder:(NSCoder *)aCoder;//序列化
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder;//反序列化
*/
Person.h文件
#import <Foundation/Foundation.h>
@interface Person : NSObject<NSCopying>
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *gender;
@property(nonatomic,assign)int age;
@end
Person.m文件
/*
把对象转换为字节序列的过程称为对象的序列化。
把字节序列恢复为对象的过程称为对象的反序列化。
*/
#import "Person.h"
@implementation Person
//归档(序列化)
//对person对象进行归档时,此方法执行
//对person中想要进行归档时的所有属性,进行序列化操作
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInt:self.age forKey:@"age"];
}
//反归档(反序列化)
//对person进行反归档时,该方法执行
//创建一个新的person对象,所有属性通过反序列化得到
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [self init]) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntForKey:@"age"];
}
return self;
}
@end
二、新建PCH File文件,并配置
//PrefixHeader.h
//在build Settings中搜索prefix,并配置绝对路径
//$(SRCROOT)/placeOnFile/PrefixHeader.pch
#ifndef PrefixHeader_pch
#define PrefixHeader_pch
#import "Person.h"
#endif
ViewController.m文件
#pragma mark --- Archiver
-(void)Archiver
{
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
//Document文件夹下,创建新的文件
NSString *filePath = [documentPath stringByAppendingPathComponent:@"customFile"];
//1.使用Data对象进行归档
NSMutableData *archiverData = [NSMutableData data];
//2.创建归档对象
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:archiverData];
//归档数据
[archiver encodeObject:@"小明" forKey:@"xm"];
[archiver encodeObject:@[@"陈杨",@"高鞠",@"社会我楠哥"] forKey:@"name"];
[archiver encodeInt:18 forKey:@"age"];
//4.完成归档
[archiver finishEncoding];
//5.将归档写入
BOOL result = [archiverData writeToFile:filePath atomically:YES];
if (result) {
NSLog(@"归档成功:%@",filePath);
} else {
NSLog(@"失败");
}
//反归档
//拿到数据文件
NSData *unarchiverData = [NSData dataWithContentsOfFile:filePath];
//进行反归档
NSKeyedUnarchiver *unarchver = [[NSKeyedUnarchiver alloc]initForReadingWithData:unarchiverData];
NSString *stringData = [unarchver decodeObjectForKey:@"xm"];
NSLog(@"%@, %@",stringData,filePath);
}
#pragma mark --- NSUserDefaults
-(void)writeNSUserDef
{
/*
NSUserDefaults是一个单例,只有这一个实例,可以用于数据永久储存
可以储存:NSNumber,NSString,NSDate,NSArry...
*/
// NSArray *array = @[@"banana",@"666",@"orange",@"apple"];
// NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
// //存
// [user setObject:array forKey:@"userArrays"];
// //取
// NSArray *arr = [user objectForKey:@"userArrays"];
// NSLog(@"%@",arr);
//注意: 要保证每一个key的唯一性,如果不唯一会覆盖
//NSUserDefaults:存储对象不可变,如果存储对像可变,要将存储对象变为不可变
NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"123",@"456",@"789", nil];
NSArray *array1 = [NSArray arrayWithArray:mutableArray];
//存储
NSUserDefaults *user2 = [NSUserDefaults standardUserDefaults];
[user2 setObject:array1 forKey:@"我是不可变的!"];
//自定义数据类型
Person *per = [Person new];
per.name = @"国祥";
per.gender = @"未知";
per.age = 12;
//创建存放person的数组
NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:0];
//将person类型转换成NSData类型
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:per];
//将date存放到数组中
[dataArray addObject:data];
//创建
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
[user setObject:data forKey:@"person"];
NSLog(@"%@",user);
//取出数据
NSUserDefaults *user3 = [NSUserDefaults standardUserDefaults];
NSData *data1 = [user3 objectForKey:@"person"];
Person *per1 = [NSKeyedUnarchiver unarchiveObjectWithData:data1];
NSLog(@"%@",per1);
}
#pragma mark --- 复杂对象写入文件
-(void)archiver
{
//$(SRCROOT)/placeOnFile/placeOnFile/PrefixHeader.pch
Person *per = [Person new];
per.gender = @"男";
per.age = 18;
NSString *path = NSHomeDirectory();
NSLog(@"%@",path);
path = [path stringByAppendingPathComponent:@"singeDog.txt"];
//1.准备存储数据的对象
NSMutableData *data = [NSMutableData data];
//2.创建归档对象
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
//3.开始归档
[archiver encodeObject:per forKey:@"person"];
//4.完成归档
[archiver finishEncoding];
//5.写入到文件中
BOOL result = [data writeToFile:path atomically:YES];
if (result) {
NSLog(@"归档成功:%@",path);
}else{
NSLog(@"归档失败");
}
#pragma mark --- 反归档/反序列化/解码/
//准备解档路径
NSData *mydata = [NSData dataWithContentsOfFile:path];
//创建反归档对象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:mydata];
//反归档
Person *per1 = [Person new];
per1 = [unarchiver decodeObjectForKey:@"person"];
//完成反归档
[unarchiver finishDecoding];
//测试
NSLog(@"%@",per1.name);
}