CacheManager类作用:管理自定义信息的存储、获取、修改、清除
UserModel类作用:需要存储的model数据,遵循NSCoding协议
数据修改和存储示例:
UserModel *model = CacheManager.shareInstance.model;
model.nickname = @"test";
model.mobile = @"123";
[CacheManager.shareInstance saveCenterModel];
数据的获取示例:
NSLog(@"nickname-%@,mobil-%@",CacheManager.shareInstance.model.nickname,CacheManager.shareInstance.model.mobile);
//nickname-test,mobil-123
CacheManager.h:
@class UserModel;
@interface CacheManager : NSObject
// 实例化
+(instancetype)shareInstance;
/** 用户信息 */
@property (nonatomic, strong)UserModel *model;
// 退出登录清除用户信息
- (void)clearUserInfo;
//储存用户数据
- (void)saveCenterModel;
//获取模型
- (UserModel *)getUserInfoModelData;
@end
CacheManager.m:
#import "CacheManager.h"
#import "UserModel.h"
static CacheManager *_singleInstance = nil;
@implementation CacheManager
+(instancetype)shareInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (_singleInstance == nil) {
_singleInstance = [[self alloc]init];
[_singleInstance fetchCenterModel];
}
});
return _singleInstance;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_singleInstance = [super allocWithZone:zone];
});
return _singleInstance;
}
-(id)copyWithZone:(NSZone *)zone
{
return _singleInstance;
}
-(id)mutableCopyWithZone:(NSZone *)zone {
return _singleInstance;
}
-(NSString *)GetDocumentPath
{
NSArray *Paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [Paths objectAtIndex:0];
return path;
}
#pragma mark - getter
/** 存储用户信息至本地 */
- (void)saveCenterModel{
if (!self.model) return;
NSString *targetPath = [[self GetDocumentPath] stringByAppendingPathComponent:@"yuserinfo.data"];
[NSKeyedArchiver archiveRootObject:self.model toFile:targetPath];
}
/** 获取本地用户信息 */
- (void)fetchCenterModel{
NSString *targetPath = [[self GetDocumentPath] stringByAppendingPathComponent:@"yuserinfo.data"];
UserModel * userinfo = [NSKeyedUnarchiver unarchiveObjectWithFile:targetPath];
if (!userinfo) userinfo = [UserModel new];
self.model = userinfo;
}
//获取本地模型数据
- (UserModel *)getUserInfoModelData{
NSString *targetPath = [[self GetDocumentPath] stringByAppendingPathComponent:@"yuserinfo.data"];
UserModel *userinfo = [NSKeyedUnarchiver unarchiveObjectWithFile:targetPath];
if (!userinfo) userinfo = [UserModel new];
return userinfo;
}
// 清空缓存用户model
- (void)clearUserInfo{
self.model = [UserModel new];
[self saveCenterModel];
}
@end
UserModel.h:
@interface UserModel : NSObject
@property (nonatomic, copy) NSString *nickname;
@property (nonatomic, copy) NSString *mobile;
@end
UserModel.m:
#import "UserModel.h"
#import <objc/runtime.h>
@implementation UserModel
//NSCoding协议方法:将需要归档的属性进行归档
- (void)encodeWithCoder:(NSCoder *)aCoder{
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([UserModel class], &count);
for(int i=0; i<count; i++){
Ivar ivar = ivars[i];
const char *name = ivar_getName(ivar);
const char *type = ivar_getTypeEncoding(ivar);
NSString *key =@(name);
id value = [self valueForKey:key];
if (strcmp(type, "q")==0 || strcmp(type, "d")==0) {
NSNumber *num = (NSNumber *)value;
[aCoder encodeInteger:num.integerValue forKey:key];
} else {
[aCoder encodeObject:value forKey:key];
}
}
free(ivars);
}
//NSCoding协议方法:将需要解档的属性进行解档
- (id)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]) {
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([UserModel class], &count);
for(int i=0; i<count; i++){
Ivar ivar = ivars[i];
const char *name = ivar_getName(ivar);
const char *type = ivar_getTypeEncoding(ivar);
NSString *key =@(name);
if (strcmp(type, "q")==0 || strcmp(type, "d")==0) {
NSInteger value = [aDecoder decodeIntegerForKey:key];
if (value) {
[self setValue:@(value) forKey:key];
}
} else {
NSString *value = [aDecoder decodeObjectForKey:key];
if (value && ![value isEqual:[NSNull null]]) {
[self setValue:value forKey:key];
}
}
}
free(ivars);
}
return self;
}
@end