Forked from mramsden/NSDictionary+QueryStringBuilder.h
Created
August 24, 2011 08:02
-
-
Save neolee/1167537 to your computer and use it in GitHub Desktop.
Creating a query string from an NSDictionary.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSDictionary *queryParameters = [NSDictionary dictionaryWithValuesAndKeys:1, @"page", nil, @"enabled", 25, @"size", nil]; | |
[queryParameters queryString]; // This will return "?page=1&enabled&size=25" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSDictionary (QueryStringBuilder) | |
- (NSString *)queryString; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSDictionary+QueryStringBuilder.h" | |
NSString * escapeString(NSString *string) | |
{ | |
NSString *s = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, | |
(CFStringRef)unencodedString, | |
NULL, | |
(CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ", | |
kCFStringEncodingUTF8); | |
return [s autorelease]; | |
} | |
@implementation NSDictionary (QueryStringBuilder) | |
- (NSString *)queryString | |
{ | |
NSMutableString *queryString = nil; | |
NSArray *keys = [self allKeys]; | |
if ([keys count] > 0) { | |
for (id key in keys) { | |
id value = [self objectForKey:key]; | |
if (nil == queryString) { | |
queryString = [[[NSMutableString alloc] init] autorelease]; | |
[queryString appendFormat:@"?"]; | |
} else { | |
[queryString appendFormat:@"&"]; | |
} | |
if (nil != key && nil != value) { | |
[queryString appendFormat:@"%@=%@", escapeString(key), escapeString(value)]; | |
} else if (nil != key) { | |
[queryString appendFormat:@"%@", escapeString(key)]; | |
} | |
} | |
} | |
return queryString; | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSDictionary (QueryStringBuilder) | |
- (NSString *)queryString; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSDictionary+QueryStringBuilder.h" | |
NSString * escapeString(NSString *unencodedString) | |
{ | |
NSString *s = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, | |
(CFStringRef)unencodedString, | |
NULL, | |
(CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ", | |
kCFStringEncodingUTF8); | |
return [s autorelease]; | |
} | |
@implementation NSDictionary (QueryStringBuilder) | |
- (NSString *)queryString | |
{ | |
NSMutableString *queryString = nil; | |
NSArray *keys = [self allKeys]; | |
if ([keys count] > 0) { | |
for (id key in keys) { | |
id value = [self objectForKey:key]; | |
if (nil == queryString) { | |
queryString = [[[NSMutableString alloc] init] autorelease]; | |
[queryString appendFormat:@"?"]; | |
} else { | |
[queryString appendFormat:@"&"]; | |
} | |
if (nil != key && nil != value) { | |
[queryString appendFormat:@"%@=%@", escapeString(key), escapeString(value)]; | |
} else if (nil != key) { | |
[queryString appendFormat:@"%@", escapeString(key)]; | |
} | |
} | |
} | |
return queryString; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In your escapeString function parameter is named string but it is called unencodedString in the body of the function.