-
Notifications
You must be signed in to change notification settings - Fork 1
/
NSDictionary+Convenience.m
109 lines (85 loc) · 3.25 KB
/
NSDictionary+Convenience.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
//
// NSDictionary+Convenience.m
// Miso
//
// Copyright 2010 Bazaar Labs, Inc. All rights reserved.
//
#import "NSDictionary+Convenience.h"
#import "NSString+Convenience.h"
@implementation NSDictionary (Convenience)
+ (NSDictionary *)dictionaryWithParameterString:(NSString *)parameterString {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithCapacity:5];
NSArray *queryElements = [parameterString componentsSeparatedByString:@"&"];
for (NSString *element in queryElements) {
NSArray *keyVal = [element componentsSeparatedByString:@"="];
if ([keyVal count] <= 1) {
continue;
}
NSString *key = [keyVal objectAtIndex:0];
NSString *value = [[keyVal lastObject] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[dictionary setObject:value forKey:key];
}
return [dictionary autorelease];
}
+ (NSDictionary *)queryParamsWithUrl:(NSString *)url {
NSArray *components = [url componentsSeparatedByString:@"?"];
if ([components count] == 2) {
return [NSDictionary dictionaryWithParameterString:[components objectAtIndex:1]];
}
return nil;
}
- (id)objectOrNilForKey:(id)key {
id object = [self objectForKey:key];
if ((NSNull *) object == [NSNull null]) {
return nil;
}
return object;
}
- (id)valueOrNilForKeyPath:(id)keyPath {
id object = [self valueForKeyPath:keyPath];
if ((NSNull *)object == [NSNull null]) {
return nil;
}
return object;
}
- (id)stringForKey:(id)key {
id object = [self objectOrNilForKey:key];
if ([object isKindOfClass:[NSString class]])
return object;
else if ([object isKindOfClass:[NSNumber class]])
return [object stringValue];
return object;
}
- (NSNumber *)numberForKey:(id)key {
id object = [self objectOrNilForKey:key];
if ([object isKindOfClass:[NSString class]])
return [NSNumber numberWithInt:[object intValue]];
else if ([object isKindOfClass:[NSNumber class]])
return object;
return object;
}
- (NSString *)queryString {
NSString *queryString = @"";
NSArray *keys = [self allKeys];
for (int i=0; i < [self count]; i++) {
NSString *key = [keys objectAtIndex:i];
id value = [self objectForKey:key];
if ([value isKindOfClass:[NSString class]]) {
NSString *param = [NSString stringWithFormat:@"%@=%@", key, [[self objectForKey:key] stringWithPercentEscape]];
queryString = [queryString stringByAppendingString:param];
} else if ([value isKindOfClass:[NSArray class]] && ([value count] > 0)) {
for (int j = 0; j < [value count]; j++) {
NSString *param = [NSString stringWithFormat:@"%@[]=%@", key, [value objectAtIndex:j]];
queryString = [queryString stringByAppendingString:param];
if (j < [value count] - 1)
queryString = [queryString stringByAppendingString:@"&"];
}
} else if ([value isKindOfClass:[NSArray class]] && ([value count] == 0)) {
queryString = [queryString stringByAppendingFormat:@"%@[]=", key];
}
if (i < [self count] - 1)
queryString = [queryString stringByAppendingString:@"&"];
}
return queryString;
}
@end