NSUserDefaults和plist保存数据再本地时,都不能保存model,只能保存一些基本的数据类型,如果想要保存model的话,我们一般会想到使用NSKeyedUnarchiver来保存。
第一步,创建我们的model
#import
@interface Account : NSObject
@property (nonatomic, copy) NSString *phone; //手机号
@property (nonatomic, copy) NSString *uid; //用户id
@property (nonatomic, copy) NSString *nickname;
@property (nonatomic, copy) NSString *password;
@property (nonatomic, copy) NSString *tel; //座机
@property (nonatomic, copy) NSString *last_login;
//@property (nonatomic, copy) NSString *new_phone;
@property (nonatomic, copy) NSString *uthumb; //头像地址
@property (nonatomic, copy) NSString *province;
@property (nonatomic, copy) NSString *city;
@property (nonatomic, copy) NSString *area;
@property (nonatomic, copy) NSData *photoData;
@end
#import "Account.h"
@implementation Account
+ (NSDictionary *)replacedKeyFromPropertyName
{
return @{@"uid": @"id"};
}
#pragma mark 归档的时候调用
MJCodingImplementation
@end
创建我们的单例的简写模式(.h文件)
// .h
#define single_interface(class) + (class *)shared##class;
// \ 代表下一行也属于宏
// ## 是分隔符
#define single_implementation(class) \
static class *_instance; \
\
+ (class *)shared##class \
{ \
if (_instance == nil) { \
_instance = [[self alloc] init]; \
} \
return _instance; \
} \
\
+ (id)allocWithZone:(NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
}
// AccountTool.h
//
#import
#import "Account.h"
#import "SingleTon.h"
@interface AccountTool : NSObject
single_interface(AccountTool)
- (void)saveAccount:(Account *)account;
- (void)removeAccount;
// 获得当前账号
@property (nonatomic, readonly) Account *account;
@end
//
// AccountTool.m
//
#import "AccountTool.h"
// 文件路径---->>获取分类的沙盒文件路径
#define kFile [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"account.data"]
@implementation AccountTool
single_implementation(AccountTool)
- (instancetype)init
{
if (self = [super init]) {
//解归档该路径下的文件
_account = [NSKeyedUnarchiver unarchiveObjectWithFile:kFile];
}
return self;
}
- (void)saveAccount:(Account *)account
{
_account = account;
//将数据归档,如果前面的文件不存在,就会在归档数据时创建
[NSKeyedArchiver archiveRootObject:account toFile:kFile];
}
- (void)removeAccount
{
if ([[NSFileManager defaultManager] fileExistsAtPath:kFile]) {
[[NSFileManager defaultManager]removeItemAtPath:kFile error:nil];
}
}
@end
//如何使用工具类呢?????
if ([AccountTool sharedAccountTool].account.uid == nil || ! kUserLogin || [[AccountTool sharedAccountTool].account.uid isKindOfClass:[NSNull class]]){
//本地未找到数据
}else {
//本地找到数据了
}
//登录后将数据保存到本地
//保存用户信息,利用mj工具类,将数据保存到model中,这里也可以使用其他的工具类,比如jsonmodel
Account *currentAccount = [Account mj_objectWithKeyValues:userInfo];
[[AccountTool sharedAccountTool] saveAccount:currentAccount];
//保存用户头像
Account *currentAccout = [AccountTool sharedAccountTool].account;
currentAccout.uthumb = responseObj[@"data"][@"uthumb"];
currentAccount.nickname = value;
[[AccountTool sharedAccountTool]saveAccount:currentAccout];
当然,我们也可以使用NSUserDefaults将数据一条一条的保存起来,但是这样的操作比较多,没有我们的这个工具类更加直观,容易使用。