美文网首页
iOS-OC使用YYCache实现数据的存储和修改

iOS-OC使用YYCache实现数据的存储和修改

作者: Simple_Code | 来源:发表于2018-02-10 16:09 被阅读255次

    1.存储的model

    #import <Foundation/Foundation.h>
    #import "MJExtension.h"
    
    @interface UserModel : NSObject
    // 用户信息
    @property (nonatomic, copy) NSString *name;//姓名
    @property (nonatomic, copy) NSString *gender;//性别
    @property (nonatomic, copy) NSString *age;//年龄
    @property (nonatomic, copy) NSString *height;//身高
    
    @end
    
    #import "UserModel.h"
    @implementation UserModel
    MJCodingImplementation; //自定义对象归档
    
    @end
    

    2.Manager

    #import <Foundation/Foundation.h>
    #import "UserModel.h"
    
    @interface UserManager : NSObject
    
    // 单利
    + (instancetype)manager;
    // 用户信息
    @property (nonatomic, strong) UserModel *userModel;
    // 存储用户字典数据
    - (void)updateUserInfo:(id)userInfo;
    // 更改用户属性值
    - (void)updateValue:(id)value forKey:(NSString *)key;
    @end
    
    
    #import "UserManager.h"
    #import "YYCache.h"
    #import "UserModel.h"
    #import "MJExtension.h"
    
    @interface UserManager()
    @property(nonatomic,strong) YYCache *userCache;//YYCache对象
    @property(nonatomic,strong) NSMutableDictionary *userInfoDict;//用户信息字典
    @end
    
    @implementation UserManager
    
    #pragma mark - 假懒加载
    - (NSMutableDictionary *)userInfoDict {
        if (!_userInfoDict) {
            id value = [self.userCache objectForKey:@"userInfo"];
            NSMutableDictionary *userInfoDict = [NSMutableDictionary dictionaryWithDictionary:(NSDictionary *)value];
            return userInfoDict;
        }
        return _userInfoDict;
    }
    
    - (UserModel *)userModel {
        if (!_userModel) {
            if (self.userInfoDict) {
                UserModel *employeeModel = [UserModel mj_objectWithKeyValues:self.userInfoDict];
                return employeeModel;
            }
        }
        return _userModel;
    }
    
    #pragma mark - 单利 - 创建管理对象
    + (instancetype)manager {
        static UserManager *instance = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            instance = [[UserManager alloc] init];
        });
        return instance;
    }
    
    - (instancetype)init {
        self = [super init];
        if (self) {
            self.userCache = [[YYCache alloc] initWithName:@"app.userInfo"];
        }
        return self;
    }
    
    #pragma mark - 存储用户字典数据
    - (void)updateUserInfo:(id)userInfo {
        [self.userCache setObject:userInfo forKey:@"userInfo"];
    }
    
    #pragma mark - 更改用户属性值
    - (void)updateValue:(id)value forKey:(NSString *)key{
        
        NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:self.userInfoDict];
        [tempDic setValue:value forKey:key];
        [self updateUserInfo:tempDic];
    }
    

    3.使用

    #import "YYCacheViewController.h"
    #import "UserManager.h"
    
    #define UserModel [UserManager manager].userModel
    #define UserInfoManager [UserManager manager]
    
    
    @interface YYCacheViewController ()
    
    @end
    
    @implementation YYCacheViewController
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        NSLog(@"######%@",@"存储地址");
        NSLog(@"%@",NSHomeDirectory());
        
        NSLog(@"%@",@"已经存储的信息");
        NSLog(@"%@",UserModel.name);
        NSLog(@"%@",UserModel.gender);
        NSLog(@"%@",UserModel.age);
        
        NSDictionary *userInfo = @{@"name":@"张三",
                                   @"gender":@"男",
                                   @"age":@"18"};
        [UserInfoManager updateUserInfo:userInfo];
        
        NSLog(@"######%@",@"存储新的信息");
        NSLog(@"%@",UserModel.name);
        NSLog(@"%@",UserModel.gender);
        NSLog(@"%@",UserModel.age);
        
        NSLog(@"######%@",@"更新信息");
        [UserInfoManager updateValue:@"改变为张四" forKey:@"name"];
        [UserInfoManager updateValue:@"改变为30" forKey:@"age"];
        [UserInfoManager updateValue:@"改变为男" forKey:@"gender"];
        [UserInfoManager updateValue:@"新添加的身高为170" forKey:@"height"];
        NSLog(@"%@",UserModel.name);
        NSLog(@"%@",UserModel.gender);
        NSLog(@"%@",UserModel.age);
        NSLog(@"%@",UserModel.height);
        
        NSLog(@"%@",UserModel.height);
    }
    

    相关文章

      网友评论

          本文标题:iOS-OC使用YYCache实现数据的存储和修改

          本文链接:https://www.haomeiwen.com/subject/znzctftx.html