1、归档处理的数据类型有NSString,NSArrry,NSData,NSDictionary
2、数据模型要使用归档必须实现<NSSecureCoding,NSCoding>协议
以数据模型举例
保存数据
if (@available(iOS 12.0,*)) {
NSError *error;
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:token requiringSecureCoding:YES error:&error];
if (error) {
NSLog(@"token保存失败%@",error);
} else {
[data writeToFile:[self tokenFilePath] atomically:YES];
}
} else {
[NSKeyedArchiver archiveRootObject:token toFile:[self tokenFilePath]];
}
复制代码
读取数据
if (@available(iOS 12.0,*)) {
NSData *data = [NSData dataWithContentsOfFile:[self tokenFilePath]];
NSError *error;
Token *token = [NSKeyedUnarchiver unarchivedObjectOfClass:Token.class fromData:data error:&error];
if (error) {
NSLog(@"token获取失败%@",error);
}
return token;
} else {
Token *token = [NSKeyedUnarchiver unarchiveObjectWithFile:[self tokenFilePath]];
return token;
}
复制代码
创建沙盒路径
+ (NSString *)tokenFilePath {
NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [pathDocuments stringByAppendingPathComponent:@"xxx.plist"];
return path;
}
复制代码
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END






















![[桜井宁宁]COS和泉纱雾超可爱写真福利集-一一网](https://www.proyy.com/skycj/data/images/2020-12-13/4d3cf227a85d7e79f5d6b4efb6bde3e8.jpg)
![[桜井宁宁] 爆乳奶牛少女cos写真-一一网](https://www.proyy.com/skycj/data/images/2020-12-13/d40483e126fcf567894e89c65eaca655.jpg)
