Skip to content

Instantly share code, notes, and snippets.

@Andrewmika
Last active July 19, 2018 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Andrewmika/d7d862ae9ceb4aa51dce6c8a4f0b8e3b to your computer and use it in GitHub Desktop.
Save Andrewmika/d7d862ae9ceb4aa51dce6c8a4f0b8e3b to your computer and use it in GitHub Desktop.
URL extension
- (NSDictionary *)dictQuery {
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
NSString *str = self.query;
if (str.length && [str rangeOfString:@"="].location != NSNotFound) {
NSArray *keyValuePairs = [str componentsSeparatedByString:@"&"];
for (NSString *keyValuePair in keyValuePairs) {
NSArray *pair = [keyValuePair componentsSeparatedByString:@"="];
// don't assume we actually got a real key=value pair. start by assuming we only got @[key] before checking count
NSString *paramValue = pair.count == 2 ? pair[1] : @"";
// CFURLCreateStringByReplacingPercentEscapesUsingEncoding may return NULL
parameters[pair[0]] = paramValue ?: @"";
}
}
return parameters;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment