-
Notifications
You must be signed in to change notification settings - Fork 1
/
NSDictionary_TLCommon.m
117 lines (103 loc) · 3.35 KB
/
NSDictionary_TLCommon.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
117
//
// NSDictionary_TLCommon.m
// TLCommon
//
// Created by Joshua Bleecher Snyder on 9/8/09.
//
#import "NSDictionary_TLCommon.h"
#import "NSString_TLCommon.h"
@implementation NSDictionary (TLCommon)
- (NSString *)queryString {
NSMutableString *queryString = [NSMutableString string];
for(id key in self) {
NSString *encodedKey = [[key description] stringByURLEncodingAllCharacters];
id value = [self objectForKey:key];
if([value isKindOfClass:[NSArray class]]) {
for(id obj in value) {
[queryString appendFormat:@"%@=%@",
encodedKey,
[[obj description] stringByURLEncodingAllCharacters]
];
[queryString appendString:@"&"];
}
} else if([value isKindOfClass:[NSDictionary class]]) {
for(id subkey in value) {
[queryString appendFormat:@"%@%5B%@%5D=%@",
encodedKey,
[[subkey description] stringByURLEncodingAllCharacters],
[[[value objectForKey:subkey] description] stringByURLEncodingAllCharacters]
];
[queryString appendString:@"&"];
}
} else if([value isKindOfClass:[NSNull class]]) {
[queryString appendFormat:@"%@=",
encodedKey
];
[queryString appendString:@"&"];
} else {
[queryString appendFormat:@"%@=%@",
encodedKey,
[[value description] stringByURLEncodingAllCharacters]
];
[queryString appendString:@"&"];
}
}
if([queryString length] > 0) {
[queryString deleteCharactersInRange:NSMakeRange([queryString length] - 1, 1)]; // remove trailing &
}
return queryString;
}
- (id)objectOfClass:(Class)desiredClass forKey:(NSString *)key {
id obj = [self objectForKey:key];
if(![obj isKindOfClass:desiredClass]) {
obj = nil;
}
return obj;
}
- (NSString *)stringForKey:(NSString *)key {
return (NSString *)[self objectOfClass:[NSString class] forKey:key];
}
- (NSArray *)arrayForKey:(NSString *)key {
return (NSArray *)[self objectOfClass:[NSArray class] forKey:key];
}
- (NSNumber *)numberForKey:(NSString *)key {
return (NSNumber *)[self objectOfClass:[NSNumber class] forKey:key];
}
- (NSDictionary *)dictionaryForKey:(NSString *)key {
return (NSDictionary *)[self objectOfClass:[NSDictionary class] forKey:key];
}
- (NSError *)errorForKey:(NSString *)key {
return (NSError *)[self objectOfClass:[NSError class] forKey:key];
}
- (id)nonNullObjectForKey:(NSString *)key {
id obj = [self objectForKey:key];
if(obj == [NSNull null]) {
obj = nil;
}
return obj;
}
+ (NSDictionary *)dictionaryWithQueryString:(NSString *)queryString {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSArray *components = [queryString componentsSeparatedByString:@"&"];
for(NSString *component in components) {
if([component length] > 0) {
NSRange equalsPosition = [component rangeOfString:@"="];
NSString *key = nil;
NSString *value = nil;
if(equalsPosition.location == NSNotFound) {
key = [key stringByURLDecodingAllCharacters];
} else {
key = [[component substringToIndex:equalsPosition.location] stringByURLDecodingAllCharacters];
value = [[component substringFromIndex:NSMaxRange(equalsPosition)] stringByURLDecodingAllCharacters];
}
if(!value) {
value = @"";
}
if(key) {
[dict setObject:value forKey:key];
}
}
}
return dict;
}
@end