Lee Sugden Lee Sugden - 5 months ago 27
Objective-C Question

NSURL set from variables

A pretty simple question i am sure, but i don't know how to go about doing such a thing. At current i $post variable 1 and variable 2 to the server and return some son data this is pretty slow but it worked, then it all went down hill when i started to have execution issues on the server side. Rented server so can't change php.ini file. So i have came up with this idea as a work around.

Would it be possible to direct to my download source, if the link itself contained variables collected from my label.text rather than writing each individual link out for each group (127 to be precise). For instance variable1 would equal "hell" and variable to would equal "red" therefore my NSURL would point to www.test.com/hell/red.php

Is this possible or is there some other way of doing this?

NSURL *url = [NSURL URLWithString:@"http://www.test.com/$variable1/$variable2.php"];


So now i know that this is possible, is some form or way, how can i resolve the following errors that i am receiving? For what i understand i simply can't have a / between the two variables and i can't have the .json file extension included on the end of the url.
enter image description here

enter image description here
If you require anymore code don't hesitate to ask.

Thank you very much in advance for any help!

Answer

The string you are creating the NSURL with is a normal NSString. To use variables in a NSString, you can use:

[NSString stringWithFormat:@"http://www.test.com/%@/%@.php", variable1, variable2];

%@ is a placeholder for a string. variable1 and variable2 must be NSStrings

The line that creates could look like this:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.test.com/%@/%@.php", variable1, variable2]];

Or:

NSString * urlString = [NSString stringWithFormat:@"http://www.test.com/%@/%@.php", variable1, variable2];
NSURL * url = [NSURL URLWithString:urlString];