forked from cheesemaker/toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUUDictionary.m
More file actions
240 lines (201 loc) · 5.12 KB
/
UUDictionary.m
File metadata and controls
240 lines (201 loc) · 5.12 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//
// UUDictionary.m
// UUFrameworkTest
//
// Created by Ryan DeVore on 4/18/14.
//
//
#import "UUDictionary.h"
#import "UUDate.h"
#import "UUString.h"
@implementation NSDictionary (UUDictionary)
- (id) uuSafeGet:(NSString*)key
{
return [self uuSafeGet:key forClass:nil defaultValue:nil];
}
- (id) uuSafeGet:(NSString*)key forClass:(Class)forClass
{
return [self uuSafeGet:key forClass:forClass defaultValue:nil];
}
- (id) uuSafeGet:(NSString*)key forClass:(Class)forClass defaultValue:(id)defaultValue
{
id obj = [self valueForKey:key];
if (obj && ![obj isKindOfClass:[NSNull class]])
{
if (forClass == nil || [obj isKindOfClass:forClass])
{
return obj;
}
}
return defaultValue;
}
- (NSNumber*) uuSafeGetNumber:(NSString*)key
{
return [self uuSafeGetNumber:key defaultValue:nil];
}
- (NSNumber*) uuSafeGetNumber:(NSString*)key defaultValue:(NSNumber*)defaultValue
{
id node = [self uuSafeGet:key];
if (node)
{
if ([node isKindOfClass:[NSNumber class]])
{
return node;
}
else if ([node isKindOfClass:[NSString class]])
{
NSNumberFormatter* f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
id val = [f numberFromString:node];
if (val)
{
return val;
}
}
}
return defaultValue;
}
- (NSString*) uuSafeGetString:(NSString*)key
{
return [self uuSafeGetString:key defaultValue:nil];
}
- (NSString*) uuSafeGetString:(NSString*)key defaultValue:(NSString*)defaultValue
{
return [self uuSafeGet:key forClass:[NSString class] defaultValue:defaultValue];
}
- (NSDate*) uuSafeGetDate:(NSString*)key formatter:(NSDateFormatter*)formatter
{
id node = [self uuSafeGetString:key];
if (node && formatter)
{
return [formatter dateFromString:node];
}
else
{
return nil;
}
}
- (NSDictionary*) uuSafeGetDictionary:(NSString*)key
{
return [self uuSafeGet:key forClass:[NSDictionary class]];
}
- (NSArray*) uuSafeGetArray:(NSString*)key
{
return [self uuSafeGet:key forClass:[NSArray class]];
}
- (NSData*) uuSafeGetData:(NSString*)key
{
return [self uuSafeGetData:key defaultValue:nil];
}
- (NSData*) uuSafeGetData:(NSString*)key defaultValue:(NSData*)defaultValue
{
return [self uuSafeGet:key forClass:[NSData class] defaultValue:defaultValue];
}
- (NSData*) uuSafeGetDataFromBase64String:(NSString*)key
{
NSString* base64String = [self uuSafeGetString:key];
if (base64String)
{
return [[NSData alloc] initWithBase64EncodedString:base64String options:0];
}
else
{
return nil;
}
}
@end
@implementation NSDictionary (UUHttpDictionary)
- (NSString*) uuBuildQueryString
{
NSDictionary* dictionary = self;
NSMutableString* queryStringArgs = [NSMutableString string];
if (dictionary && dictionary.count > 0)
{
[queryStringArgs appendString:@"?"];
// Append query string args
int count = 0;
NSArray* keys = [dictionary allKeys];
for (int i = 0; i < dictionary.count; i++)
{
NSString* key = [keys objectAtIndex:i];
id rawVal = [dictionary objectForKey:key];
NSString* val = nil;
if ([rawVal isKindOfClass:[NSString class]])
{
val = (NSString*)rawVal;
}
else if ([rawVal isKindOfClass:[NSNumber class]])
{
val = [rawVal stringValue];
}
if (val != nil)
{
if (count > 0)
{
[queryStringArgs appendString:@"&"];
}
NSString* formattedKey = [key uuUrlEncoded];
NSString* formattedValue = [val uuUrlEncoded];
[queryStringArgs appendFormat:@"%@=%@", formattedKey, formattedValue];
++count;
}
}
}
return queryStringArgs;
}
- (NSString*) uuToJsonString
{
NSString* str = nil;
@try
{
NSError* err = nil;
NSData* data = [NSJSONSerialization dataWithJSONObject:self options:0 error:&err];
if (err != nil)
{
return nil;
}
str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
@catch (NSException *exception)
{
str = nil;
}
@finally
{
return str;
}
}
- (NSData*) uuToJson
{
NSData* data = nil;
@try
{
NSError* err = nil;
data = [NSJSONSerialization dataWithJSONObject:self options:0 error:&err];
}
@catch (NSException *exception)
{
data = nil;
}
@finally
{
return data;
}
}
@end
@implementation NSMutableDictionary (UUMutableDictionary)
- (void) uuSafeSetValue:(nullable id)value forKey:(nonnull NSString*)key
{
if (key)
{
[self setValue:value forKey:key];
}
}
- (void) uuSafeRemove:(id)key
{
if (key)
{
[self removeObjectForKey:key];
}
}
@end