Forked from halsk/NSURL+dictionaryFromQueryString.h
Last active
December 23, 2015 15:49
-
-
Save kawanet/6657628 to your computer and use it in GitHub Desktop.
val にも = が入っていたり、key/val に + があった場合に対応してみました。
val が不正で stringByReplacingPercentEscapesUsingEncoding が例外を出す場合もあるけど未対応。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSURL (dictionaryFromQueryString) | |
-(NSDictionary *) dictionaryFromQueryString; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) { | |
NSRange range = [pair rangeOfString:@"="]; | |
NSString *key = range.length ? [pair substringToIndex:range.location] : pair; | |
NSString *val = range.length ? [pair substringFromIndex:range.location+1] : @""; | |
key = [key stringByReplacingOccurrencesOfString:@"+" withString:@" "]; | |
val = [val stringByReplacingOccurrencesOfString:@"+" withString:@" "]; | |
key = [key stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
val = [val 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