Creating a URL query string using an NSDictionary

Posted on May 14, 2011

4


I finally got round to making this into a category. It’s a simple function for turning a dictionary into a query string, ready for appending to the end of a URL.

// NSDictionary+QueryStringBuilder.h

#import <Foundation/Foundation.h>

@interface NSDictionary (QueryStringBuilder)

- (NSString *)queryString;

@end
// NSDictionary+QueryStringBuilder.m

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

Using this category is very simple;

NSDictionary *queryParameters = [NSDictionary dictionaryWithValuesAndKeys:1, @"page", nil, @"enabled", 25, @"size", nil];
[queryParameters queryString]; // This will return "?page=1&enabled&size=25"

You can get the sourcecode as a gist.

Advertisements
Tagged: , ,