以下代码以前可以在swift 2.2编译,现在swift 3.0不能编译了。我们如何解决这个问题?
Error: Binary operator '===' cannot be applied to operands of type 'Any?' and 'UIBarButtonItem!'
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if sender === saveButton { // Error!
// ...
} else if sender === closeButton { // Error!
// ...
}
}
最佳答案
正如错误信息所说。在 Swift 3 中,Objecitve-C id
被导入为 Any
,您不能为 Any
调用任何操作,包括 ===
,没有显式转换。
试试这个:
if sender as AnyObject? === saveButton {
(其他sender
比较都一样。)
请记住,在 Swift 3 中,as AnyObject
已成为风险最高的操作之一,您不应在其他情况下使用 as AnyObject
。
关于swift - 二元运算符 '===' 不能应用于类型 'Any?' 和 'UIBarButtonItem!' 的操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39666213/