PDA

View Full Version : Elegant solution for getting querystring params from NSURL?




printf
Aug 18, 2010, 02:42 AM
string parsing has never been my forte, and the few results i've found googling are a bit unwieldy and not malleable enough for any future custom revisions i might want to make.

are there any simple/elegant ways to pull out the individual parameters for inspection?

for example, if i want to get the values of a, b, and c:
http://www.mydomain.com/?a=1&b=2&c=3



printf
Aug 18, 2010, 12:32 PM
btw, the solution can use std string, cstring, nsstring... doesn't matter to me.

dejo
Aug 18, 2010, 01:03 PM
I would look into using regex for something like this.

chown33
Aug 18, 2010, 01:20 PM
Do you need URL-decoding on the parameters? If so, you'll have to apply it.


This seems like a basic problem in decomposition or factoring.

Break it down into smaller sub-problems, until you have solvable sub-problems. It's fundamental to programming, or almost any other engineering or technical problem-solving field:
http://en.wikipedia.org/wiki/Decomposition_(computer_science)


You could start by creating an NSURL, then calling its query method. That should return the "a=1&b=2&c=3" part. You've now separated the part you want from the part you don't care about.

Or you could find the first "?" in the URL string and take everything after that point. There are no unescaped ?'s in a URL, so you shouldn't have to worry about that. Again, this is simply identifying and separating the part you want from the part you don't care about.


With the NSString containing "a=1&b=2&c=3", you can use NSString's componentsSeparatedByCharactersInSet method, with a character-set containing only the '&'. This will get you an NSArray of NSStrings, each one being a "a=1", "b=2", etc. Decomposition again.

For each string in the array, split it at the '=' into a name and a value. More decomposition.

For each split string, you could use the first part as a dictionary key, the second part as a dictionary value, and add them to a dictionary. Then you can repeatedly find values by their name (key) without having to repeat the decompositions.


If that's not malleable enough, then you'll have to describe what kind of malleability you want. No one has any way to guess your specifications. You have to specify them.

Luke Redpath
Aug 18, 2010, 01:21 PM
I wouldn't bother with a regex.

You can extract the query string by creating an NSURL object from the string and using the query property.

You can then just split it on & to get the key pairs and each key pair by = to get the key and value. That should generally do the trick.

Edit: I just remembered that I have some categories that I use in a few of my projects that you are welcome to use. http://github.com/lukeredpath/LRResty/tree/master/Categories/

There are methods for getting a dictionary from a query string and back again with support for escaping and nested parameters.

printf
Aug 18, 2010, 01:32 PM
With the NSString containing "a=1&b=2&c=3", you can use NSString's componentsSeparatedByCharactersInSet method, with a character-set containing only the '&'. This will get you an NSArray of NSStrings, each one being a "a=1", "b=2", etc. Decomposition again.

For each string in the array, split it at the '=' into a name and a value. More decomposition.

For each split string, you could use the first part as a dictionary key, the second part as a dictionary value, and add them to a dictionary. Then you can repeatedly find values by their name (key) without having to repeat the decompositions.


yeah, this is kind of what i'm after. guess i wasn't sure if there was a more 'out of the box solution' for this type of object. in javascript, i'd split the '&', then the '=' and call it a day, but it's obviously more complicated in c, and i'm still newish to parsing strings in obj-c.


Edit: I just remembered that I have some categories that I use in a few of my projects that you are welcome to use. http://github.com/lukeredpath/LRResty/tree/master/Categories/

There are methods for getting a dictionary from a query string and back again with support for escaping and nested parameters.

I'll take a look, thx!

bonatto
Jan 27, 2011, 07:12 AM
This was my tentative of finding a relatively simple way to parse a query string.


NSArray *parameters = [[url query] componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"=&"]];
NSMutableDictionary *keyValueParm = [NSMutableDictionary dictionary];

for (int i = 0; i < [parameters count]; i=i+2) {
[keyValueParm setObject:[parameters objectAtIndex:i+1] forKey:[parameters objectAtIndex:i]];
}


Hope it helps.