How to concatenate a string into a URL on Xcode (Objective-c)?

I need to replace "CurrentLocation" with the value in the following URL. "CurrentLocation" is a predefined string and gives a dynamic value (I mean a different location / city name each time). I need help. How to do this 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 help me with this. Thanks!!

+4
source share
2 answers

You can use the static stringWithFormat message in the NSString class. This will create an NSString object with auto-implementation using the formatting you specified (using the 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]; 
+12
source

nice stringByAppendingString method, here is more info on apple website.

 NSString *errorTag = @"Error: "; NSString *errorString = @"premature end of file."; NSString *errorMessage = [errorTag stringByAppendingString:errorString]; 
+1
source

Source: https://habr.com/ru/post/1347015/


All Articles