性能完善,使用简单。
过程:
-
建立一个模型,一般即为你要存储数据的模型,我这里是用户数据。
~1.1 模型的数据类型,模型要遵守<NSCoding>
@property(nonatomic, copy)NSString *userName;
@property(nonatomic, copy)NSString *userPhone;
@property(nonatomic, assign)NSInteger userAge;
+ (SJUserModel *)userWithName:(NSString *)name age:(NSInteger)age phone:(NSString *)phone;
~1.2 实现模型赋值方法
+ (SJUserModel *)userWithName:(NSString *)name age:(NSInteger)age phone:(NSString *)phone
{
SJUserModel *model = [[self alloc] init];
model.userName = name;
model.userAge = age;
model.userPhone = phone;
return model;
}
~1.3 实现归档自动触发 和 解归档自动触发方法
- (void)encodeWithCoder:(NSCoder *)aCoder{
//保存值
[aCoder encodeObject:self.userName forKey:@"userName"];
[aCoder encodeObject:self.userPhone forKey:@"userPhone"];
[aCoder encodeInteger:self.userAge forKey:@"userAge"];
}- (instancetype)initWithCoder:(NSCoder *)aDecoder{ if (self = [super init]) { //取值 self.userName = [aDecoder decodeObjectForKey:@"userName"]; self.userPhone = [aDecoder decodeObjectForKey:@"userPhone"]; self.userAge = [aDecoder decodeIntegerForKey:@"userAge"]; } return self; }
-
-
2.建立归档类
~2.1 声明外部接口方法
/用户信息归档/
+ (void)archiverUserInfo:(SJUserModel )model;
/用户信息读档/
+ (SJUserModel )currentArchiverUserInfo;
~2.2 具体实现归档、解归档部分
/归档/
+ (void)archiverUserInfo:(SJUserModel )model{
[self archiverModel:model fileName:kUserInfoFileName dataName:kUserInfoDataKey];
}
/解归档/
+(SJUserModel )currentArchiverUserInfo{
//如果内存中有UserModel,直接读取,否则去磁盘读取
if ([self memoryUserInfo] != nil) {
return [self memoryUserInfo];
}
return [self unArchiverWithFieldName:kUserInfoFileName dataName:kUserInfoDataKey];
}
/获取当前内存中的用户信息/
+ (SJUserModel )memoryUserInfo{
AppDelegate delegate = [UIApplication sharedApplication].delegate;
return delegate.currentUserModel;
}
/路径/
+ (NSString *)filePathComponentName:(NSString *)fileName{
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0] stringByAppendingPathComponent:fileName];
}
+ (void)archiverModel:(NSObject *)model fileName:(NSString *)fileName dataName:(NSString *)dataName{
NSString *dataFilePath = [self filePathComponentName:fileName];
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];[archiver encodeObject:model forKey:dataName]; [archiver finishEncoding]; [data writeToFile:dataFilePath atomically:YES]; } + (id)unArchiverWithFieldName:(NSString *)fileName dataName:(NSString *)dataName{ NSString *filePath = [self filePathComponentName:fileName]; NSData *data = [[NSMutableData alloc] initWithContentsOfFile:filePath]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; id model = [unarchiver decodeObjectForKey:dataName]; [unarchiver finishDecoding]; if (model == nil) { NSLog(@"读档信息为空"); } return model; }
- 3.至此,封装完毕。使用部分
~3.1 登录时给模型赋值,归档模型数据
<pre>SJUserModel *userModel = [SJUserModel userWithName:self.nameTF.text age:self.ageTF.text.integerValue phone:self.phoneTF.text];
[SJArchive archiverUserInfo:userModel];</pre>~3.2下一次进APP时从内存中或者磁盘解归档数据
<pre>SJUserModel *model = [SJArchive currentArchiverUserInfo];
self.nameLB.text = model.userName;
self.ageLB.text = [NSString stringWithFormat:@"%ld",model.userAge];
if ([self.ageLB.text isEqualToString:@"0"])
self.ageLB.text = @"";
}
self.phoneLB.text = model.userPhone;</pre>
~3.2退出登录时,给当前用户数据模型置"空",然后归档一次"空"模型
<pre>SJUserModel *model = [SJArchive currentArchiverUserInfo];
model.userName = @"";
model.userPhone = @"";
model.userAge = @"".integerValue;
//归档
[SJArchive archiverUserInfo:model];</pre>