gabac gabac - 3 months ago 16
Objective-C Question

Parse NSURL query property

I have a URL like

myApp://action/1?parameter=2&secondparameter=3


With the property query I get following part of my
URL


parameter=2&secondparameter=3


Is there any way easy to put this in a
NSDictionary
or an
Array
?

Thx a lot

Answer

Something like that:

NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
for (NSString *param in [url componentsSeparatedByString:@"&"]) {
  NSArray *elts = [param componentsSeparatedByString:@"="];
  if([elts count] < 2) continue;
  [params setObject:[elts lastObject] forKey:[elts firstObject]];
}

Note : This is sample code. All error cases are not managed.