-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreferenceManager.m
More file actions
131 lines (106 loc) · 3.75 KB
/
PreferenceManager.m
File metadata and controls
131 lines (106 loc) · 3.75 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
//
// PreferenceManager.m
//
// Created by Doug Anson.
// Copyright 2010 AnsonWorks.com. All rights reserved.
//
#import "PreferenceManager.h"
@implementation PreferenceManager
@synthesize mAppName;
@synthesize mPrefMgr;
- (id) init:(NSString *)appName {
self = [super init];
// setup ourselves
self.mAppName = [[NSString alloc] initWithString:appName];
self.mPrefMgr = [NSUserDefaults standardUserDefaults];
// return self
return self;
}
- (void) setPreference:(NSString *)name withIntValue:(int)value {
NSString *sval = [[NSString alloc] initWithFormat:@"%d",value];
[self setPreference:name withValue:sval];
}
- (void) setPreference:(NSString *)name withBooleanValue:(BOOL)value {
NSString *sval = @"NO";
if (value) sval = @"YES";
[self setPreference:name withValue:sval];
}
- (void) setPreference:(NSString *)name withValue:(NSString *)value {
@try{
if (name != nil && [name length] > 0 && value != nil && [value length] > 0) {
// store the value
[self.mPrefMgr setObject:value forKey:name];
// make sure that the value is written out
for (int i=0;i<2;++i)
[self.mPrefMgr synchronize];
}
else if (name != nil && [name length] > 0) {
// we just store a blank string
[self.mPrefMgr setObject:@"" forKey:name];
// make sure that the value is written out
for (int i=0;i<2;++i)
[self.mPrefMgr synchronize];
}
else {
// Invalid parameters - ignore
;
}
}
@catch (NSException *ex) {
NSLog(@"%@: PrefMgr Exception(SET) %@ Message: %@",self.mAppName,ex.name, ex.reason);
}
}
- (int) getIntPreference:(NSString *)name {
return [self getIntPreference:name withDefault:0];
}
- (int) getIntPreference:(NSString *)name withDefault:(int)def {
int ival = 0;
NSString *sdef = [[NSString alloc] initWithFormat:@"%d",def];
NSString *sval = [self getPreference:name withDefault:sdef];
if (sval != nil)
ival = [sval intValue];
return ival;
}
- (BOOL) getBooleanPreference:(NSString *)name {
return [self getBooleanPreference:name withDefault:NO];
}
- (BOOL) getBooleanPreference:(NSString *)name withDefault:(BOOL)def {
BOOL bval = NO;
NSString *sdef = @"NO";
if (def == YES) sdef = @"YES";
NSString *sval = [self getPreference:name withDefault:sdef];
@try {
if (sval != nil) bval = [sval boolValue];
}
@catch (NSException *ex) {
NSLog(@"%@: PrefMgr Exception(GET) %@ Message: %@",self.mAppName, ex.name, ex.reason);
}
return bval;
}
- (NSString *) getPreference:(NSString *)name {
return [self getPreference:name withDefault:@""];
}
- (NSString *) getPreference:(NSString *)name withDefault:(NSString *)def {
NSString *value = nil;
@try {
if (name != nil && [name length] > 4) {
value = [self.mPrefMgr stringForKey:name];
if (value != nil)
value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (value == nil || [value length] == 0) {
value = [def stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
}
}
@catch (NSException *ex) {
NSLog(@"%@: PrefMgr Exception(GET) %@ Message: %@",self.mAppName,ex.name, ex.reason);
}
return value;
}
- (NSString *) getPreference:(NSString *)name withDefaultInt:(int)def {
NSString *sdef = [[NSString alloc] initWithFormat:@"%d",def];
NSString *result = [self getPreference:name withDefault:sdef];
if (result == nil) result = sdef;
return result;
}
@end