无法在 Xcode 中构建并出现错误“[方法] 已被明确标记为不可用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26763641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:05:58  来源:igfitidea点击:

Can't build in Xcode with error "[method] has been explicitly marked unavailable"

iosxcodeswift

提问by Andreas

I've changed nothing in my project and all of a sudden I get these errors when building to my device:

我在我的项目中没有做任何更改,并且在构建到我的设备时突然出现这些错误:

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

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

on this line:

在这一行:

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

The quick help says the method (and the class, NSSURLComponents) has been available since "iOS (8.0 and later)". The docs say "Available in iOS 7.0 and later". Strange.

快速帮助说明该方法(和类NSSURLComponents)自“iOS(8.0 及更高版本)”以来一直可用。文档说“在 iOS 7.0 及更高版本中可用”。奇怪的。

If I follow the directions in the error and use the constructor instead, I get compiler errors when accessing urlComponents.schemeand urlComponents.URL. Not that I had high hopes -- my code is untouched and has compiled fine with no problems for weeks.

如果我按照错误中的说明操作并改用构造函数,则在访问urlComponents.scheme和时会出现编译器错误urlComponents.URL。并不是说我抱有很高的期望——我的代码没有受到影响,并且已经编译好几周了,没有出现任何问题。

  • I cleaned the product and the build folder
  • I reverted to a known working commit
  • I closed the project, restarted Xcode, restarted the OS, reinstalled Xcode
  • Tried running both debug and release
  • Tried changing deployment target to 8.0
  • Tried running in simulator and on device
  • 我清理了产品和构建文件夹
  • 我恢复到一个已知的工作提交
  • 我关闭了项目,重新启动了 Xcode,重新启动了操作系统,重新安装了 Xcode
  • 尝试同时运行调试和发布
  • 尝试将部署目标更改为 8.0
  • 尝试在模拟器和设备上运行

The only thing that looks different in Xcode is that below my project name it says "ios sdk 8.1". I can only remember having seen 8.0 there before, but I've never changed it and there's no option to change it back. Don't know why it would make any difference though.

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

Xcode Version 6.1 (6A1052d)

Xcode 6.1 版 (6A1052d)

回答by Martin R

As indicated in the error message, the constructor is NSURLComponents(URL:resolvingAgainstBaseURL:), in your case

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

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

Note also that this is a "failable initializer" (now) and returns an optional. So you have to unwrap the result explicitly or test with optional binding:

还要注意,这是一个“失败的初始化程序”(现在)并返回一个可选的。因此,您必须显式打开结果或使用可选绑定进行测试:

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