Last active
August 29, 2015 13:58
-
-
Save virasio/10253030 to your computer and use it in GitHub Desktop.
NSURL+Dictionaries — Returns properties query and fragment of NSURL as NSDictionary
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
@interface NSURL (QueryDictionary) | |
- (NSDictionary *)queryDictionary; | |
@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+QueryDictionary.h" | |
@implementation NSURL (QueryDictionary) | |
- (NSDictionary *)queryDictionary | |
{ | |
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; | |
NSArray *parameters = [self.query componentsSeparatedByString:@"&"]; | |
for (NSString *parameter in parameters) { | |
NSArray *parts = [parameter componentsSeparatedByString:@"="]; | |
NSString *key = [[parts objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
if ([parts count] > 1) | |
{ | |
id value = [[parts objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
[dict setObject:value forKey:key]; | |
} | |
} | |
return [NSDictionary dictionaryWithDictionary:dict]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment