我正在使用这段代码将我的按钮链接分配给 wiki 页面,同时捕获 UILabel 中的 countryName.text 作为 URL 的一部分,但是当我按下它时 Xcode 给我一个错误。这是代码:
- (IBAction)openWiki:(id)sender {
NSString *sampleUrl = [[NSString alloc] initWithFormat:@"http://en.wikipedia.org/wiki/%@%@",self.countryName.text];
NSURL *wikiUrl = [[NSURL alloc] initWithString:sampleUrl];
[[UIApplication sharedApplication] openURL:wikiUrl];}
提前致谢。
最佳答案
在您的格式中,您需要两个参数,但只给出一个:
@"http://en.wikipedia.org/wiki/%@%@",self.countryName.text
// ^^
删除一个说明符:
- (IBAction)openWiki:(id)sender {
NSString *sampleUrl = [[NSString alloc]
initWithFormat:@"http://en.wikipedia.org/wiki/%@",self.countryName.text];
// ^^
NSURL *wikiUrl = [[NSURL alloc] initWithString:sampleUrl];
[[UIApplication sharedApplication] openURL:wikiUrl];
}
关于iPhone NSURL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10462289/