【问题标题】:iPhone NSURL erroriPhone NSURL 错误
【发布时间】:2012-05-05 13:23:21
【问题描述】:

我正在使用此代码将按钮的链接分配给 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];}

提前致谢。

【问题讨论】:

  • 您在 XCODE 上遇到的错误是什么?
  • 去掉 %@ 符号之一。

标签: iphone objective-c ios xcode nsurl


【解决方案1】:

在您的格式中,您需要两个参数,但只给出一个:

@"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];
}

【讨论】:

  • 这有帮助,但是当 self.countryName.text 类似于“San Marino”(两个词而不是一个词)时,链接将无法打开:(
  • 没关系,它处理一个完整的 NSString。您之前可能会对其进行编码,但那是另一回事。
  • 好的,太好了!非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-25
  • 2011-03-20
  • 2011-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多