-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNSUserDefaults+KissNSUserDefaults.m
More file actions
184 lines (163 loc) · 7.68 KB
/
NSUserDefaults+KissNSUserDefaults.m
File metadata and controls
184 lines (163 loc) · 7.68 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//
// NSUserDefaults+KissNSUserDefaults.m
// KissNSUserDefaults
//
// Created by Chen Xian'an on 1/1/13.
// Copyright (c) 2013 lazyapps. All rights reserved.
//
#import "NSUserDefaults+KissNSUserDefaults.h"
#import <objc/runtime.h>
NSString * const KissNSUserDefaultsDidChangeNotification = @"KissNSUserDefaultsDidChangeNotification";
NSString * const KissNSUserDefaultsUserInfoKey = @"KissNSUserDefaultsUserInfoKey";
NSString * const KissNSUserDefaultsUserInfoObjectValue = @"KissNSUserDefaultsUserInfoObjectValue";
#define SETTER_OBJ_IMP(type, setter, userDefaultsKey, value) \
imp_implementationWithBlock(^void(id sender, type value){ \
if (value){ \
[sender setter:value forKey:userDefaultsKey]; \
} else { \
[sender removeObjectForKey:userDefaultsKey]; \
} \
[[NSNotificationCenter defaultCenter] postNotificationName:KissNSUserDefaultsDidChangeNotification object:nil userInfo:@{KissNSUserDefaultsUserInfoKey : userDefaultsKey, KissNSUserDefaultsUserInfoObjectValue : value ?: [NSNull null]}] ;\
})
#define SETTER_IMP(type, setter, userDefaultsKey, value) \
imp_implementationWithBlock(^void(id sender, type value){ \
[sender setter:value forKey:userDefaultsKey]; \
[[NSNotificationCenter defaultCenter] postNotificationName:KissNSUserDefaultsDidChangeNotification object:nil userInfo:@{KissNSUserDefaultsUserInfoKey : userDefaultsKey, KissNSUserDefaultsUserInfoObjectValue : @(value) ?: [NSNull null]}] ;\
})
#define GETTER_IMP(type, getter, userDefaultsKey) \
imp_implementationWithBlock(^type (id sender){ \
return [sender getter:userDefaultsKey]; \
})
#if defined(__LP64__) && __LP64__
#define KISS_NSINTEGER_TYPE @"q"
#else
#define KISS_NSINTEGER_TYPE @"i"
#endif
#if !defined(OBJC_HIDE_64) && TARGET_OS_IPHONE && __LP64__
#define KISS_BOOL_TYPE @"B"
#else
#define KISS_BOOL_TYPE @"c"
#endif
@implementation NSUserDefaults (KissNSUserDefaults)
/**
Simply declare properties in your NSUserDefaults category's header, and @dynamic them in @implementation. Then run this in your NSUserDefaults category's `+(void)load`.
*/
+ (void)kiss_setup
{
[self kiss_setupWithCustomKeys:nil];
}
+ (void)kiss_setupWithCustomKeys:(NSDictionary *)propertyKeyPairs
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
@autoreleasepool {
NSDictionary *getters;
NSDictionary *setters;
NSDictionary *types;
[self kiss_getDynamicGetters:&getters setters:&setters types:&types];
for (id key in getters){
NSString *getterName = getters[key];
NSString *setterName = setters[key];
if (!setterName){
NSMutableString *mStr = [key mutableCopy];
[mStr deleteCharactersInRange:NSMakeRange(0, 1)];
NSString *part = [NSString stringWithFormat:@"set%@", [[key substringWithRange:NSMakeRange(0, 1)] uppercaseString]];
[mStr insertString:part atIndex:0];
[mStr appendString:@":"];
setterName = mStr;
}
NSString *type = types[key];
NSString *userDefaultsKey = propertyKeyPairs && propertyKeyPairs[key] ? propertyKeyPairs[key] : key;
IMP imp = NULL;
if ([type isEqualToString:@"@"])
imp = SETTER_OBJ_IMP(id, setObject, userDefaultsKey, value);
else if ([type isEqualToString:KISS_BOOL_TYPE])
imp = SETTER_IMP(BOOL, setBool, userDefaultsKey, value);
else if ([type isEqualToString:@"d"])
imp = SETTER_IMP(double, setDouble, userDefaultsKey, value);
else if ([type isEqualToString:@"f"])
imp = SETTER_IMP(float, setFloat, userDefaultsKey, value);
else if ([type isEqualToString:KISS_NSINTEGER_TYPE])
imp = SETTER_IMP(NSInteger, setInteger, userDefaultsKey, value);
else
@throw [NSException exceptionWithName:@"KissNSUserDefaults" reason:[NSString stringWithFormat:@"type %@ hasn't implemented yet", type] userInfo:nil];
SEL sel = NSSelectorFromString(setterName);
const char *methodType = [[NSString stringWithFormat:@"v@:%@", types[key]] UTF8String];
class_addMethod(self, sel, imp, methodType);
if ([type isEqualToString:@"@"])
imp = GETTER_IMP(id, objectForKey, userDefaultsKey);
else if ([type isEqualToString:KISS_BOOL_TYPE])
imp = GETTER_IMP(BOOL, boolForKey, userDefaultsKey);
else if ([type isEqualToString:@"d"])
imp = GETTER_IMP(double, doubleForKey, userDefaultsKey);
else if ([type isEqualToString:@"f"])
imp = GETTER_IMP(float, floatForKey, userDefaultsKey);
else if ([type isEqualToString:KISS_NSINTEGER_TYPE])
imp = GETTER_IMP(NSInteger, integerForKey, userDefaultsKey);
else
@throw [NSException exceptionWithName:@"KissNSUserDefaults" reason:[NSString stringWithFormat:@"type %@ hasn't implemented yet", type] userInfo:nil];
sel = NSSelectorFromString(getterName);
methodType = [[NSString stringWithFormat:@"%@@:", types[key]] UTF8String];
class_addMethod(self, sel, imp, methodType);
}
#if TARGET_OS_IPHONE
#ifdef UIKIT_EXTERN
NSArray *notes = @[UIApplicationWillTerminateNotification, UIApplicationDidEnterBackgroundNotification];
#define hasNotes
#endif
#else
#ifdef _APPKITDEFINES_H
NSArray *notes = @[NSApplicationWillTerminateNotification, NSApplicationWillResignActiveNotification];
#define hasNotes
#endif
#endif
#ifdef hasNotes
[notes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
[[NSNotificationCenter defaultCenter] addObserverForName:obj object:nil queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification *note){
[[self standardUserDefaults] synchronize];
}];
}];
#endif
}
});
}
+ (NSString *)kiss_getAccessorName:(NSString *)accessor
{
NSRange r = NSMakeRange(2, [accessor length]-2);
if ((r = [accessor rangeOfString:@"," options:0 range:r]).location != NSNotFound)
return [accessor substringWithRange:NSMakeRange(2, r.location-2)];
return [accessor substringFromIndex:2];
}
+ (void)kiss_getDynamicGetters:(NSDictionary **)outGetters
setters:(NSDictionary **)outSetters
types:(NSDictionary **)outTypes
{
NSMutableDictionary *getters = [NSMutableDictionary dictionary];
NSMutableDictionary *setters = [NSMutableDictionary dictionary];
NSMutableDictionary *types = [NSMutableDictionary dictionary];
unsigned int outCount, i;
objc_property_t *classProperties = class_copyPropertyList([self class], &outCount);
for (i=0; i<outCount; i++){
objc_property_t property = classProperties[i];
const char *propChar = property_getName(property);
if (propChar){
const char *attr = property_getAttributes(property);
if (strstr(attr, "D,")){ // only interests in dynamic property
NSString *propName = [NSString stringWithUTF8String:propChar];
char *subAttr = NULL;
if ((subAttr = strstr(attr, ",G"))) // handle custom getter
getters[propName] = [self kiss_getAccessorName:[NSString stringWithUTF8String:subAttr]];
else
getters[propName] = propName;
if ((subAttr = strstr(attr, ",S"))) // handle custom setter
setters[propName] = [self kiss_getAccessorName:[NSString stringWithUTF8String:subAttr]];
types[propName] = [[NSString stringWithUTF8String:attr] substringWithRange:NSMakeRange(1, 1)];
}
}
}
free(classProperties);
*outGetters = getters;
*outSetters = [setters count] ? setters : nil;
*outTypes = types;
}
@end