Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
57 lines (44 sloc) 1.47 KB
//
// NSDictionary+SSToolkitAdditions.m
// SSToolkit
//
// Created by Sam Soffes on 9/21/10.
// Copyright 2010-2011 Sam Soffes. All rights reserved.
//
#import "NSDictionary+SSToolkitAdditions.h"
#import "NSString+SSToolkitAdditions.h"
@implementation NSDictionary (SSToolkitAdditions)
+ (NSDictionary *)dictionaryWithFormEncodedString:(NSString *)encodedString {
NSMutableDictionary *result = [NSMutableDictionary dictionary];
NSArray *pairs = [encodedString componentsSeparatedByString:@"&"];
for (NSString *kvp in pairs) {
if ([kvp length] == 0) {
continue;
}
NSRange pos = [kvp rangeOfString:@"="];
NSString *key;
NSString *val;
if (pos.location == NSNotFound) {
key = [kvp stringByUnescapingFromURLQuery];
val = @"";
} else {
key = [[kvp substringToIndex:pos.location] stringByUnescapingFromURLQuery];
val = [[kvp substringFromIndex:pos.location + pos.length] stringByUnescapingFromURLQuery];
}
if (!key || !val) {
continue; // I'm sure this will bite my arse one day
}
[result setObject:val forKey:key];
}
return result;
}
- (NSString *)stringWithFormEncodedComponents {
NSMutableArray *arguments = [NSMutableArray arrayWithCapacity:[self count]];
for (NSString *key in self) {
[arguments addObject:[NSString stringWithFormat:@"%@=%@",
[key stringByEscapingForURLQuery],
[[[self objectForKey:key] description] stringByEscapingForURLQuery]]];
}
return [arguments componentsJoinedByString:@"&"];
}
@end
Jump to Line
Something went wrong with that request. Please try again.