Breaking News
Home / iOS / iOS Snippets / How to parse Query String into KeyValue Pair?

How to parse Query String into KeyValue Pair?

Send Email iOSThe following snippet shows how to parse the Http QueryString and extract the parameters (key/value pairs) as a dictionary object.

This is useful for working with iPhone/iPad applications that send or receive http requests.

<!>

  1.  (NSDictionary *)parseQueryString:(NSString *)query {
  2.     NSMutableDictionary *dict = [[[NSMutableDictionary alloc] initWithCapacity:6] autorelease];
  3.     NSArray *pairs = [query componentsSeparatedByString:@"&"];
  4.    
  5.     for (NSString *pair in pairs) {
  6.         NSArray *elements = [pair componentsSeparatedByString:@"="];
  7.         NSString *key = [[elements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  8.         NSString *val = [[elements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  9.        
  10.         [dict setObject:val forKey:key];
  11.     }
  12.     return dict;
  13. }
[pb_builder]

Check Also

EKCalendar – That account does not allow calendars to be added or removed

Usecase When I am trying to create a new Calendar programmatically using the EventKit API …

Advertisment ad adsense adlogger