如何在Xcode(Objective-c)上连接URL中的字符串?

Him*_*lay 4 javascript iphone objective-c ios

我需要将"CurrentLocation"替换为以下URL中的值."CurrentLocation"是一个预定义的字符串,它给出了一个动态值(我的意思是每次都有不同的位置/城市名称).我需要帮助,如何在Objective-C中做到这一点?

NSURL *MainURL = [NSURL URLWithString:@"http://news.google.com/news?q=location:CurrentLocation&output=rss"];
Run Code Online (Sandbox Code Playgroud)

在JavaScript中我会做这样的事情;

var a="Google"; var b=".com"; var c=".np"; var d=a+b+c; document.write(d);
Run Code Online (Sandbox Code Playgroud)

请有人帮我这个.谢谢!!

Red*_*ing 12

您可以在NSString类上使用stringWithFormat静态消息.这将使用您指定的格式创建一个自动释放的 NSString对象(使用printf样式格式字符串).所以对于你的情况:

// 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];
Run Code Online (Sandbox Code Playgroud)