Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NSURL category to parser GET parameters from the URL into a NSDictionary #objective-c #utils #url
#import <Foundation/Foundation.h>
@interface NSURL (QueryParser)
-(NSDictionary*)queryDictionary;
@end
#import "NSURL+QueryParser.h"
@implementation NSURL (QueryParser)
-(NSDictionary *)queryDictionary
{
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
for (NSString *param in [[self query] componentsSeparatedByString:@"&"]) {
NSArray *parts = [param componentsSeparatedByString:@"="];
if([parts count] < 2) continue;
[params setObject:[parts objectAtIndex:1] forKey:[parts objectAtIndex:0]];
}
return params;
}
@end
@trojanfoe
Copy link

Code taken from this stackoverflow question, without attribution: http://stackoverflow.com/questions/3997976/parse-nsurl-query-property

@dalewking
Copy link

Probably should replace percent escapes in the keys and values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment