This file contains 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
- (NSDictionary *)parseQueryString:(NSString *)query { | |
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:6]; | |
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; | |
} |
NSString *qs = @"username=hank&age=27&blog=whhnote.blogspot.com"; NSDictionary *queryParams = [self parseQueryString:qs]; NSLog(@"%@", queryParams); NSLog(@"%@", [queryParams objectForKey:@"username"]); // get username from dictSo, you will get a dictionary look like
{ age = 27; blog = "whhnote.blogspot.com"; username = hank; }