Objective-C
Xcode
iOS

NSURLのクエリー情報をNSDictionaryに変換する

objective-c
@interface NSURL (nsUrlWithQuery)
-(NSDictionary *)queryAsDictionary;
@end

@implementation NSURL (nsUrlWithQuery)

- (NSDictionary *)queryAsDictionary {
    NSLog(@"in queryAsDictionary");
    NSArray *components = [[self query] componentsSeparatedByString:@"&"];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    for ( NSString *component in components ) {
        NSArray *keyAndValues = [component componentsSeparatedByString:@"="];
        [parameters setObject:[keyAndValues objectAtIndex:1] forKey:[keyAndValues objectAtIndex:0]];
    }
    return parameters;
}

@end
objective-c
NSDictionary *query = [[NSURL URLWithString:urlString] queryAsDictionary];
NSLog(@"query: %@", query);