JLChen
2021-04-30 a5247b61d585627a1a7b1e1f35f34de9f0af9fba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
//  Copyright © 2016年 dahua. All rights reserved.
//
 
#import <LCBaseModule/LCStore.h>
#import <LCBaseModule/DHFileManager.h>
 
@interface LCStore() {
    NSMutableDictionary *_storeDic;
    
    NSString *_localPath;
}
 
@end
 
@implementation LCStore
 
 
- (instancetype)initWithLocalPath:(NSString *)path {
    if (self = [super init]) {
        _localPath = path;
        NSLog(@"🍎🍎🍎 %@:: Init with local path - %@", NSStringFromClass([self class]), path);
        _storeDic = [NSMutableDictionary dictionaryWithContentsOfFile:_localPath];;
        if (_storeDic == nil) {
            _storeDic = [NSMutableDictionary new];
        }
    }
    return self;
}
 
- (instancetype)init {
    if (self = [super init]) {
        NSString *configfilepath = [DHFileManager configFilePath];
        NSLog(@"🍎🍎🍎 %@:: Init with config file path - %@", NSStringFromClass([self class]), configfilepath);
        _storeDic = [NSMutableDictionary dictionaryWithContentsOfFile:configfilepath];;
        if (_storeDic == nil) {
            _storeDic = [NSMutableDictionary new];
        }
    }
    return self;
}
 
- (NSDictionary *)dicStore {
    return [_storeDic copy];
}
 
#pragma mark - 数据存储
- (id)getObjByKey:(NSString *)key {
    return _storeDic[key];
}
 
- (void)saveObj:(id)value withKey:(NSString *)key {
    @synchronized(self)
    {
        if (key == nil) {
            return;
        }
        
        //如果与现在的值一样  没必要再写一次文件
        if (_storeDic[key]!= nil && [value isKindOfClass:[NSNumber class]] && [_storeDic[key] isKindOfClass:[NSNumber class]]) {
            NSNumber *num1 = (NSNumber *)value;
            NSNumber *num2 = (NSNumber *)_storeDic[key];
            if ([num1 isEqualToNumber:num2]) {
                return ;
            }
        }
        
        _storeDic[key] = value;
        [self saveDictionary];
    }
    
}
 
- (BOOL)saveDictionary {
    return [_storeDic writeToFile:_localPath atomically:YES];
}
 
- (void)saveAppended:(NSDictionary *)dictionary {
    [_storeDic addEntriesFromDictionary:dictionary];
    
    for (id key in _storeDic.allKeys) {
        [self saveObj:_storeDic[key] withKey:key];
    }
}
 
//+ (void)lc_KC_StorePassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account {
//    [YYKeychain setPassword:password forService:serviceName account:account];
//}
//
////获取密码从钥匙串
//+ (NSString *)lc_KC_PasswordForService:(NSString *)serviceName account:(NSString *)account {
//    NSString *password = [YYKeychain getPasswordForService:serviceName account:account];
//    if (!password) {
//        password = @"";
//    }
//    return password;
//}
 
////储存密码到userdefault
//+(void)lc_UD_StoreValue:(NSString *)value forKey:(NSString *)key{
//    [[NSUserDefaults standardUserDefaults] set]
//}
//
////获取值从userdefault
//+(id)lc_UD_ValueForKey:(NSString *)key{
//  return [[NSUserDefaults standardUserDefaults] objectForKey:key];
//}
 
 
@end