-
Notifications
You must be signed in to change notification settings - Fork 8
/
DefaultsDomain.m
116 lines (91 loc) · 2.43 KB
/
DefaultsDomain.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
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
#import "DefaultsDomain.h"
static NSMutableDictionary *domains = nil;
@interface ComBelkadanUtils_DefaultsDomain ()
- (id)initWithDomainName:(NSString *)domainName;
@end
@implementation ComBelkadanUtils_DefaultsDomain
@synthesize domain;
+ (void)initialize {
if (self == [ComBelkadanUtils_DefaultsDomain class]) {
domains = [[NSMutableDictionary alloc] init];
}
}
+ (ComBelkadanUtils_DefaultsDomain *)domainForName:(NSString *)domainName {
ComBelkadanUtils_DefaultsDomain *domain = [domains objectForKey:domainName];
if (!domain) {
domain = [[self alloc] initWithDomainName:domainName];
[domains setObject:domain forKey:domainName];
[domain release];
}
return domain;
}
- (id)init {
return [self initWithDomainName:[[NSBundle mainBundle] bundleIdentifier]];
}
- (id)initWithDomainName:(NSString *)domainName {
self = [super init];
if (self) {
domain = [domainName copy];
values = [[NSMutableDictionary alloc] init];
[self refresh];
}
return self;
}
- (void)dealloc {
[domain release];
[values release];
[super dealloc];
}
- (void)refresh {
[values setDictionary:[[NSUserDefaults standardUserDefaults] persistentDomainForName:self.domain]];
}
- (void)save {
if (transactionCount == 0) {
[[NSUserDefaults standardUserDefaults] setPersistentDomain:values forName:self.domain];
}
}
- (void)beginTransaction {
++transactionCount;
}
- (void)endTransaction {
NSAssert(transactionCount > 0, @"Unbalanced transaction count (more ends than begins)");
--transactionCount;
if (transactionCount == 0) [self save];
}
#pragma mark -
- (id)objectForKey:(id)key {
return [values objectForKey:key];
}
- (NSUInteger)count {
return [values count];
}
- (NSEnumerator *)keyEnumerator {
return [values keyEnumerator];
}
#pragma mark -
- (void)removeObjectForKey:(id)key {
[values removeObjectForKey:key];
[self save];
}
- (void)setObject:(id)obj forKey:(id)key {
[values setObject:obj forKey:key];
[self save];
}
- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary {
[values addEntriesFromDictionary:otherDictionary];
[self save];
}
- (void)removeAllObjects {
[values removeAllObjects];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:self.domain];
}
- (void)removeObjectsForKeys:(NSArray *)keyArray {
[values removeObjectsForKeys:keyArray];
[self save];
}
- (void)setDictionary:(NSDictionary *)otherDictionary {
[values setDictionary:otherDictionary];
[self save];
}
// TODO: setValue:forKeyPath:
@end