-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSDictionary+Utilities.mm
226 lines (183 loc) · 4.63 KB
/
NSDictionary+Utilities.mm
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//
// author: fabrice truillot de chambrier
//
// © 2009-2015, men in silicium sàrl
//
#import "OS/NSDictionary+Utilities.h"
#import "OS/NSString+Utilities.h"
#import "OS/nil.h"
@implementation NSDictionary (Utilities)
- (BOOL) isMutable
{
return [self isMutableDictionary];
}
- (BOOL) isMutableDictionary
{
return [self respondsToSelector: @selector(setObject:forKey:)];
}
- (BOOL) containsKey: (id) key
{
return [self objectForKey: key] != nil;
}
- (BOOL) containsObject: (id) object
{
NSArray* keys = [self allKeysForObject: object];
return keys.count > 0;
}
- (id) objectForKeyCaseInsensitive: (id) key
{
if ( key == nil ) return nil;
if ( not [key isKindOfClass: NSString.class] ) return nil;
for ( id mykey in self.allKeys )
{
if ( not [mykey isKindOfClass: NSString.class] ) continue;
if ( [mykey isEqualToStringCaseInsensitive: key] ) {
return [self objectForKey: mykey];
}
}
return nil;
}
- (NSDictionary*) exchangeObjectsAndKeys: (BOOL*) lossy
{
NSMutableDictionary* dictionary = [[NSMutableDictionary alloc] initWithCapacity: self.count];
BOOL isLossy = NO;
for ( id key in self.allKeys )
{
id object = [self objectForKey: key];
if ( [dictionary objectForKey: object] == nil ) {
dictionary[object] = key;
}
else {
isLossy = YES;
}
}
if ( lossy != nil ) *lossy = isLossy;
return [dictionary copy];
}
- (BOOL) hasValueForKey: (NSString*) key
{
return [self valueForKey: key] != nil;
}
- (BOOL) hasValueForKeyPath: (NSString*) path
{
if ( path == nil ) return YES;
NSRange dot = [path rangeOfString: @"." options: NSBackwardsSearch];
if ( dot.location != NSNotFound )
{
NSString* subpath = [path substringToIndex: dot.location];
NSString* key = [path substringFromIndex: dot.location + 1];
id leaf = [self valueForKeyPath: subpath];
if ( is_nil( leaf ) ) return NO;
if ( [leaf isKindOfClass: NSDictionary.class] ) {
return [leaf hasValueForKey: key];
}
else {
return NO;
}
}
else
{
return [self hasValueForKey: path];
}
}
- (id) valueForKey: (NSString*) key1 forKey: (NSString*) key2
{
id value = [self valueForKey: key1];
if ( not [value respondsToSelector: @selector(valueForKey:)] ) return nil;
return [value valueForKey: key2];
}
- (NSDictionary*) entriesPassingTest: (BOOL (^)( id key, id obj, BOOL* stop )) predicate
{
NSMutableDictionary* entries = [NSMutableDictionary new];
for ( id key in self.allKeys )
{
id object = self[key];
BOOL stop = NO;
if ( predicate( key, object, &stop ) ) {
entries[key] = object;
}
if ( stop ) break;
}
return entries;
}
@end
@implementation NSMutableDictionary (Creation)
+ (instancetype) newWithCapacity: (NSUInteger) capacity
{
return [NSMutableDictionary dictionaryWithCapacity: capacity];
}
@end
@implementation NSMutableDictionary (Utilities)
- (void) intersectKeysWithDictionary: (NSDictionary*) dictionary
{
[self intersectKeysWithArray: dictionary.allKeys];
}
- (void) intersectKeysWithArray: (NSArray*) keys
{
for ( id key in self )
{
if ( not [keys containsObject: key] ) {
[self removeObjectForKey: key];
}
}
}
- (void) setValue: (id) value forKey: (NSString*) key1 forKey: (NSString*) key2
{
id value1 = [self valueForKey: key1];
if ( value1 == nil )
{
value1 = [NSMutableDictionary new];
[self setValue: value1 forKey: key1];
}
if ( [value1 isKindOfClass: NSDictionary.class] )
{
value1 = [value1 mutableCopy];
[value1 setValue: value forKey: key2];
[self setValue: value1 forKey: key1];
}
else
{
[NSException raise: NSDestinationInvalidException format: @"NSMutableDictionary setValue:forKey:forKey: couldn't set the value %@ at %@.%@", value, key1, key2];
}
}
- (void) setValue: (id) value forKeyPath: (NSString*) keyPath create: (BOOL) create
{
if ( not create )
{
[self setValue: value forKeyPath: keyPath];
return;
}
if ( [keyPath containsString: @"."] )
{
NSString* key = [keyPath substringToString: @"."];
id keyValue = [self valueForKey: key];
if ( is_not_nil( keyValue ) )
{
if ( [keyValue isKindOfClass: NSDictionary.class] )
{
keyValue = [keyValue mutableCopy];
[self setValue: keyValue forKey: key];
}
else
{
[NSException raise: NSDestinationInvalidException format: @"NSMutableDictionary setValue:forKeyPath:create: couldn't set the value %@ at %@", value, keyPath];
}
}
else
{
keyValue = [NSMutableDictionary new];
[self setValue: keyValue forKey: key];
}
if ( keyValue != nil )
{
NSString* path = [keyPath substringFromString: @"." included: NO];
// Recursion down the path
[keyValue setValue: value forKeyPath: path create: create];
}
}
else
{
[self setValue: value forKey: keyPath];
}
}
@end