-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserPreferences.m
69 lines (55 loc) · 2.05 KB
/
UserPreferences.m
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
#import "UserPreferences.h"
@implementation UserPreferences
+(NSString*)getStringWithKey:(NSString*)key {
return [[NSUserDefaults standardUserDefaults] objectForKey:key];
}
+(NSString*)getStringWithKey:(NSString*)key withDefault:(NSString*)d {
if([[NSUserDefaults standardUserDefaults] objectForKey:key] == nil) {
return d;
}
return [[NSUserDefaults standardUserDefaults] objectForKey:key];
}
+(void)setString:(NSString*)v withKey:(NSString*)key {
[[NSUserDefaults standardUserDefaults] setObject:v forKey:key];
[[NSUserDefaults standardUserDefaults] synchronize];
}
+(BOOL)getBoolWithKey:(NSString*)key {
return [[[NSUserDefaults standardUserDefaults] objectForKey:key] boolValue];
}
+(BOOL)getBoolWithKey:(NSString*)key withDefault:(BOOL)d {
if([[NSUserDefaults standardUserDefaults] objectForKey:key] == nil) {
return d;
}
return [[[NSUserDefaults standardUserDefaults] objectForKey:key] boolValue];
}
+(void)setBool:(BOOL)v withKey:(NSString*)key {
[[NSUserDefaults standardUserDefaults] setObject:v ? @"1" : @"0" forKey:key];
[[NSUserDefaults standardUserDefaults] synchronize];
}
+(int)getIntWithKey:(NSString*)key {
return (int)[[NSUserDefaults standardUserDefaults] integerForKey:key];
}
+(int)getIntWithKey:(NSString*)key withDefault:(int)d {
if([[NSUserDefaults standardUserDefaults] objectForKey:key] == nil) {
return d;
}
return (int)[[NSUserDefaults standardUserDefaults] integerForKey:key];
}
+(void)setInt:(int)v withKey:(NSString*)key {
[[NSUserDefaults standardUserDefaults] setInteger:v forKey:key];
[[NSUserDefaults standardUserDefaults] synchronize];
}
+(BOOL)keyExists:(NSString*)key {
return [[NSUserDefaults standardUserDefaults] objectForKey:key] != nil;
}
+(BOOL)keyUndefined:(NSString*)key {
return [[NSUserDefaults standardUserDefaults] objectForKey:key] == nil;
}
+(BOOL)isKeyUndefinedThenDefine:(NSString*)key {
BOOL isKeyUndefined = [UserPreferences keyUndefined:key];
if(isKeyUndefined) {
[UserPreferences setString:key withKey:key];
}
return isKeyUndefined;
}
@end