swift - 二元运算符 '===' 不能应用于类型 'Any?' 和 'UIBarButtonItem!' 的操作数

标签 swift swift3

以下代码以前可以在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/

相关文章:

ios - 无法关闭 Swift 中的导航 Controller

ios - 尝试呈现 AlertController 时的窗口层次结构消息

ios - 子类中未调用 Swift 3 ObjC 可选协议(protocol)方法

c# - 如何在 Unity3d MonoDevelop 中使用继承?

objective-c - 从 Objective-C 转换为 Swift

ios - 在 SwiftUI 中创建包含 1000 个元素的列表时出现性能问题

swift - 条件绑定(bind)的初始化程序必须具有可选类型,而不是 'String' ?

ios - 图表 lineChartDataset 舍入值

ios - 关联类型 Swift 3

iOS - 从底部填充 UIBezierPath 的动画