Skip to content

Instantly share code, notes, and snippets.

@halsk
Created February 28, 2013 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save halsk/5055274 to your computer and use it in GitHub Desktop.
Save halsk/5055274 to your computer and use it in GitHub Desktop.
URLのパラメータ部分を NSDictionary に変換する ref: http://qiita.com/items/413ec7902f48ec39821f
#import <Foundation/Foundation.h>
@interface NSURL (dictionaryFromQueryString)
-(NSDictionary *) dictionaryFromQueryString;
@end
#import "NSURL+dictionaryFromQueryString.h"
@implementation NSURL (dictionaryFromQueryString)
-(NSDictionary *) dictionaryFromQueryString{
NSString *query = [self query];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:0];
NSArray *pairs = [query componentsSeparatedByString:@"&"];
for (NSString *pair in pairs) {
NSArray *elements = [pair componentsSeparatedByString:@"="];
NSString *key = [[elements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *val = [[elements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[dict setObject:val forKey:key];
}
return dict;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment