Elegant solution for getting querystring params from NSURL?

Discussion in 'iOS Programming' started by printf, Aug 18, 2010.

  1. printf macrumors regular

    Aug 27, 2008
    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:
  2. printf thread starter macrumors regular

    Aug 27, 2008
    btw, the solution can use std string, cstring, nsstring... doesn't matter to me.
  3. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    I would look into using regex for something like this.
  4. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    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:

    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.
  5. Luke Redpath macrumors 6502a

    Nov 9, 2007
    Colchester, UK
    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.
  6. printf thread starter macrumors regular

    Aug 27, 2008
    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.

    I'll take a look, thx!
  7. bonatto macrumors newbie

    Jan 27, 2011
    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.

Share This Page