Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Creating a query string from an NSDictionary.
NSDictionary *queryParameters = [NSDictionary dictionaryWithValuesAndKeys:1, @"page", nil, @"enabled", 25, @"size", nil];
[queryParameters queryString]; // This will return "?page=1&enabled&size=25"
#import <Foundation/Foundation.h>
@interface NSDictionary (QueryStringBuilder)
- (NSString *)queryString;
@end
#import "NSDictionary+QueryStringBuilder.h"
@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:@"%@=%@", key, value];
} else if (nil != key) {
[queryString appendFormat:@"%@", key];
}
}
}
return queryString;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.