NSKeyedchiver 简单保存用户信息

2019-04-14 19:42发布

首先账号的存储路径:

#define JJTAccountPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.archive"]

模型写代码

当一个对象要归档进沙盒中时,就会调用这个方法

- (void)encodeWithCoder:(NSCoder *)encoder ;
[encoder encodeObject:self.xxx forKey:@"xxx"];

保存数据

[NSKeyedArchiver archiveRootObject:model toFile:JJTAccountPath];

模型写代码

当从沙盒中解档一个对象时(从沙盒中加载一个对象时),就会调用这个方法

- (id)initWithCoder:(NSCoder *)decoder;
if (self = [super init]) {
self.xxx = [decoder decodeObjectForKey:@"xxx"];
}

读取数据

[NSKeyedUnarchiver unarchiveObjectWithFile:JJTAccountPath];