LoginSignup
1
1

More than 5 years have passed since last update.

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

Posted at
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);
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1