Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Concatenate a string in URL on Xcode (Objective-c)?

I need to replace "CurrentLocation" with a value in following URL. "CurrentLocation" is a predefined string and it gives a dynamic value (I mean different name of location/city each time). I need help, How to do it in Objective-C ??

NSURL *MainURL = [NSURL URLWithString:@"http://news.google.com/news?q=location:CurrentLocation&output=rss"];

In JavaScript i would do something like this;

var a="Google"; var b=".com"; var c=".np"; var d=a+b+c; document.write(d);

Please somebody help me with this. Thanks!!

like image 937
Himalay Avatar asked Dec 12 '22 14:12

Himalay


2 Answers

You can use the the stringWithFormat static message on the NSString class. This will create an autoreleased NSString object using the formatting you specify (using printf style format string). So for your case:

// get the current location from somewhere
NSString * yourCurrentLocation = @"Sydney";

// create your urlString %@ is replaced with the yourCurrentLocation argument
NSString * urlString = [NSString stringWithFormat:@"http://news.google.com/news?q=location:%@&output=rss", yourCurrentLocation];
like image 111
RedBlueThing Avatar answered Dec 30 '22 00:12

RedBlueThing


also, a good method is stringByAppendingString, here is more info in the apple site.

NSString *errorTag = @"Error: ";
NSString *errorString = @"premature end of file.";
NSString *errorMessage = [errorTag stringByAppendingString:errorString];
like image 33
Cesar A. Rivas Avatar answered Dec 29 '22 23:12

Cesar A. Rivas