Skip to content

Instantly share code, notes, and snippets.

@mrtj
Created May 20, 2013 15:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mrtj/5613206 to your computer and use it in GitHub Desktop.
Save mrtj/5613206 to your computer and use it in GitHub Desktop.
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