Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 18, 2010, 02:42 AM   #1
printf
macrumors regular
 
Join Date: Aug 2008
Elegant solution for getting querystring params from NSURL?

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 is offline   0 Reply With Quote
Old Aug 18, 2010, 12:32 PM   #2
printf
Thread Starter
macrumors regular
 
Join Date: Aug 2008
btw, the solution can use std string, cstring, nsstring... doesn't matter to me.
printf is offline   0 Reply With Quote
Old Aug 18, 2010, 01:03 PM   #3
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
I would look into using regex for something like this.
__________________
dejo is online now   0 Reply With Quote
Old Aug 18, 2010, 01:20 PM   #4
chown33
macrumors 603
 
Join Date: Aug 2009
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/Decompo...mputer_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.
chown33 is offline   0 Reply With Quote
Old Aug 18, 2010, 01:21 PM   #5
Luke Redpath
macrumors 6502a
 
Join Date: Nov 2007
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/LRRest...er/Categories/

There are methods for getting a dictionary from a query string and back again with support for escaping and nested parameters.
Luke Redpath is offline   0 Reply With Quote
Old Aug 18, 2010, 01:32 PM   #6
printf
Thread Starter
macrumors regular
 
Join Date: Aug 2008
Quote:
Originally Posted by chown33 View Post
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.


Quote:
Originally Posted by Luke Redpath View Post
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/LRRest...er/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!
printf is offline   0 Reply With Quote
Old Jan 27, 2011, 07:12 AM   #7
bonatto
macrumors newbie
 
Join Date: Jan 2011
This was my tentative of finding a relatively simple way to parse a query string.

Code:
		
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.
bonatto is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Those elegant, fragile mice ... niki.t.bowden Mac Peripherals 5 Feb 7, 2014 08:39 AM
Pop up blocker in NSURL abidkhan135 Mac Programming 0 Jul 17, 2013 12:14 PM
Elegant Key Mapping Solution? julianhfaulkner Mac Peripherals 0 Oct 27, 2012 11:47 AM
NSURL Connection DidFinishLoading Called When Not Done newtoiphonesdk iPhone/iPad Programming 0 Jul 17, 2012 05:13 PM
Elegant Solution for Media Library and Backup FakeSteveJobs OS X 2 Jul 6, 2012 10:40 AM

Forum Jump

All times are GMT -5. The time now is 11:43 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC