Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 61 lines (49 sloc) 1.564 kB
f773ffd @soffes Updated comments
soffes authored
1 //
2 // NSDictionary+QueryString.h
3 // LROAuth2Client
4 //
5 // Created by Luke Redpath on 14/05/2010.
6 // Copyright 2010 LJR Software Limited. All rights reserved.
7 //
8
9 #import "NSDictionary+QueryString.h"
5683846 @lukeredpath Renamed categories.
authored
10 #import "NSString+QueryString.h"
c8cc914 @lukeredpath Fixed some silly errors, make sure the category headers are exported
authored
11
5683846 @lukeredpath Renamed categories.
authored
12 @implementation NSDictionary (QueryString)
14daa62 @lukeredpath Use a more robust query string -> NSDictionary class that deals with …
authored
13
14 + (NSDictionary *)dictionaryWithFormEncodedString:(NSString *)encodedString
15 {
16 NSMutableDictionary* result = [NSMutableDictionary dictionary];
17 NSArray* pairs = [encodedString componentsSeparatedByString:@"&"];
18
19 for (NSString* kvp in pairs)
20 {
21 if ([kvp length] == 0)
22 continue;
23
24 NSRange pos = [kvp rangeOfString:@"="];
25 NSString *key;
26 NSString *val;
27
28 if (pos.location == NSNotFound)
29 {
30 key = [kvp stringByUnescapingFromURLQuery];
31 val = @"";
32 }
33 else
34 {
35 key = [[kvp substringToIndex:pos.location] stringByUnescapingFromURLQuery];
36 val = [[kvp substringFromIndex:pos.location + pos.length] stringByUnescapingFromURLQuery];
37 }
38
39 if (!key || !val)
40 continue; // I'm sure this will bite my arse one day
41
42 [result setObject:val forKey:key];
43 }
44 return result;
45 }
46
47 - (NSString *)stringWithFormEncodedComponents
48 {
49 NSMutableArray* arguments = [NSMutableArray arrayWithCapacity:[self count]];
50 for (NSString* key in self)
51 {
52 [arguments addObject:[NSString stringWithFormat:@"%@=%@",
53 [key stringByEscapingForURLQuery],
54 [[[self objectForKey:key] description] stringByEscapingForURLQuery]]];
55 }
56
57 return [arguments componentsJoinedByString:@"&"];
58 }
59
60 @end
Something went wrong with that request. Please try again.