前言
1.每次做本地数据的缓存是都比较头痛。
2.用NSUserDefaults方便快捷,但是每次用key取的时候都担心key值写错,并且缓存数据过多时候会有一大堆key值,总感觉有点.......
3.用归档NSKeyedArchiver,需要实现NSCoding协议,每增加一个属性都要在协议里实现,万一忘记了,缓存就会失败。
4.本文用的方法是对NSUserDefaults的进一步封装,可以直接将服务器返回的数据字典NSDictionary缓存到本地,并且以模型方式返回。
1 创建模型类:类型UserUser
2 .h文件
#import <Foundation/Foundation.h>
@interface UserUser : NSObject
@property (nonatomic,copy)NSString * name;
@property (nonatomic,copy)NSString * sex;
@property (nonatomic,copy)NSString * age;
//单例方法,取出缓存的数据模型
+(instancetype)shareUserUser;
//用字典缓存用户数据
-(void)saveWithUserDic:(NSDictionary *)userDic;
//保存用户数据-(对当前模型修改后用)
-(void)save;
//移除本地缓存的数据
-(void)remove;
@end
3 .m文件
#import "UserUser.h"
#import "MJExtension.h"
#import <objc/runtime.h>
#define cacheKey @"user"//缓存key自己随意修改
@implementation UserUser
+(instancetype)shareUserUser{
static UserUser * user;
if(user==nil){
user=[[UserUser alloc]init];
//获取本地缓存
NSDictionary *userDic=[self objectForKey:cacheKey];
if (userDic) {
user=[UserUser mj_objectWithKeyValues:userDic];
}
}
return user;
}
//用字典缓存用户数据
-(void)saveWithUserDic:(NSDictionary *)userDic{
[self setObject:userDic forKey:cacheKey];
UserUser*user=[UserUser mj_objectWithKeyValues:userDic];
unsigned int count = 0;
objc_property_t *properties = class_copyPropertyList([self class], &count);
for (int i = 0; i < count; i++) {
const char *name = property_getName(properties[i]);
NSString *propertyName = [NSString stringWithUTF8String:name];
id propertyValue = [user valueForKey:propertyName];
if (propertyValue) {
[self setValue:propertyValue forKey:propertyName];
}
}
free(properties);
}
//保存用户数据-(对当前模型修改后可用)
- (void)save{
//把当前数据模型转为字典存入本地
NSDictionary *userDic=self.mj_keyValues;
[self setObject:userDic forKey:cacheKey];
}
//移除本地缓存的数据
-(void)remove{
[self setObject:@{} forKey:cacheKey];
unsigned int count = 0;
objc_property_t *properties = class_copyPropertyList([self class], &count);
for (int i = 0; i < count; i++) {
const char *name = property_getName(properties[i]);
NSString *propertyName = [NSString stringWithUTF8String:name];
[self setValue:nil forKey:propertyName];
}
free(properties);
}
#pragma mark NSUserDefaults操作
+(id)objectForKey:(NSString *)defaultName{
NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
return [userDefaults objectForKey:defaultName];
}
-(void)setObject:(id)value forKey:(NSString *)defaultName{
NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
[userDefaults setObject:value forKey:defaultName];
[userDefaults synchronize];
}
4调用
NSDictionary *dic=@{@"name":@"kuipeng",
@"sex":@"男人",
@"abc":@"qwertyuiop",
@"bcd":@"asdfghjkl",
@"cdf":@"zxcvbnm"
};
UserUser *user1=[UserUser shareUserUser];
NSLog(@"1:%@",user1.name);
NSLog(@"1:%@",user1.sex);
NSLog(@"1:%@",user1.age);
[user1 saveWithUserDic:dic];
NSLog(@"2:%@",user1.name);
NSLog(@"2:%@",user1.sex);
NSLog(@"2:%@",user1.age);
5 打印
2019-06-21 11:02:39.431787+0800 AAA[76218:5882492] 1:songkuipeng
2019-06-21 11:02:39.431870+0800 AAA[76218:5882492] 1:男
2019-06-21 11:02:39.431886+0800 AAA[76218:5882492] 1:(null)
2019-06-21 11:02:39.433117+0800 AAA[76218:5882492] 2:kuipeng
2019-06-21 11:02:39.433175+0800 AAA[76218:5882492] 2:男人
2019-06-21 11:02:39.433190+0800 AAA[76218:5882492] 2:(null)
6 其他事项
项目中有用到MJExtension请自行添加