【问题标题】:Can't build in Xcode with error "[method] has been explicitly marked unavailable"无法在 Xcode 中构建并出现错误“[method] 已被明确标记为不可用”
【发布时间】:2014-11-05 17:41:12
【问题描述】:

我的项目中没有任何更改,但在构建到我的设备时突然出现以下错误:

'componentsWithURL(_:resolvingAgainstBaseURL:)' is unavailable: use object construction 'NSURLComponents(URL:resolvingAgainstBaseURL:)'

'componentsWithURL(_:resolvingAgainstBaseURL:)' has been explicitly marked unavailable here (Foundation.NSURLComponents)

在这一行:

let urlComponents = NSURLComponents.componentsWithURL(url, resolvingAgainstBaseURL: false)

快速帮助说该方法(和类,NSSURLComponents)从“iOS(8.0 及更高版本)”开始可用。文档说“在 iOS 7.0 及更高版本中可用”。奇怪。

如果我按照错误中的说明使用构造函数,则在访问 urlComponents.schemeurlComponents.URL 时会出现编译器错误。并不是说我寄予厚望——我的代码没有受到影响,并且已经编译好几个星期了,没有任何问题。

  • 我清理了产品和构建文件夹
  • 我恢复到一个已知的工作提交
  • 我关闭了项目,重启了Xcode,重启了操作系统,重新安装了Xcode
  • 尝试同时运行调试和发布
  • 尝试将部署目标更改为 8.0
  • 尝试在模拟器和设备上运行

在 Xcode 中唯一看起来不同的是,在我的项目名称下方,它显示“ios sdk 8.1”。我只记得以前在那里见过 8.0,但我从未更改过它,也没有选择将其更改回来。不知道为什么会有所不同。

Xcode 版本 6.1 (6A1052d)

【问题讨论】:

    标签: ios xcode swift


    【解决方案1】:

    如错误消息中所示,构造函数是NSURLComponents(URL:resolvingAgainstBaseURL:),在你的情况下

    let urlComponents = NSURLComponents(URL: url, resolvingAgainstBaseURL: false);
    

    还要注意,这是一个“失败的初始化程序”(现在)并返回一个可选的。 所以你必须明确地解开结果或者使用可选绑定进行测试:

    if let urlComponents = NSURLComponents(URL: url, resolvingAgainstBaseURL: false) {
        // Use urlComponents ..
    } else {
        // failed
    }
    

    【讨论】:

    • 是的,没想到它会变成可选的。谢谢。
    猜你喜欢
    • 2021-05-16
    • 2015-08-29
    • 2022-10-25
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 2013-03-17
    • 2020-08-15
    • 1970-01-01
    相关资源
    最近更新 更多