About an hour ago I came across a question on StackOverflow about a nice way of getting the variables from a URL like “http://domain.com/action.php?var1=100&variable2=true“. I don’t really need that feature but I always wanted to investigate the NSScanner Class and even though the hour is quite late, I saw this was my opportunity to do so. So I went on to create a class that, using the NSScanner would perform the task.

The usage is simple:

URLParser *parser = [[[URLParser alloc] initWithURLString:@"http://domain.com/action.php?var1=100&variable2=true"] autorelease];
NSString *var1 = [parser valueForVariable:@"var1"];
NSLog(@"%@", var);   //100
NSString *var2 = [parser valueForVariable:@"var2"];
NSLog(@"%@", var2);   //(null)
NSString *variable2 = [parser valueForVariable:@"variable2"];
NSLog(@"%@", variable2);   //true

You have a look at the class and how the NSScanner is used in the source files below.
URLParser.m URLParser.h