Forked from kawanet/NSURL+dictionaryFromQueryString.h
Last active
August 29, 2015 14:23
-
-
Save applideveloper/71dfc69fae544e8d85c0 to your computer and use it in GitHub Desktop.
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