views:

32

answers:

2

Hi there

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

+1  A: 

Try this ;)!

NSString *query = @"parameter=2&secondparameter=3"; // replace this with [url query];
NSArray *components = [query componentsSeparatedByString:@"&"];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
for (NSString *component in components) {
    [parameters setObject:[[component componentsSeparatedByString:@"="] objectAtIndex:0] forKey:[[component componentsSeparatedByString:@"="] objectAtIndex:1]];
}
Tim van Elsloo
that's what I call easy. thx a lot!
gabac
A: 

Something like that:

NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
for (NSString* param in [url componentsSeparatedByString:@"&"]) {
  NSArray* elts = [param componentsSeparatedByString:@"="];
  [params addObject:[elts objectAtIndex:1] forKey:[elts objectAtIndex:0]];
}
Benoît