二元运算符“==="不能应用于“Any?"类型的操作数和“UIBarButtonItem!" [英] Binary operator '===' cannot be applied to operands of type 'Any?' and 'UIBarButtonItem!'

查看:20
本文介绍了二元运算符“==="不能应用于“Any?"类型的操作数和“UIBarButtonItem!"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码以前可以在 swift 2.2 中编译,现在在 swift 3.0 中不能编译了.我们如何解决这个问题?

The following code used to be able to compile in swift 2.2, no longer in swift 3.0. How do we fix this?

错误:二元运算符==="不能应用于Any?"类型的操作数和UIBarButtonItem!"

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 调用任何操作,包括 ===,没有显式转换.

As the error message is saying. In Swift 3, Objecitve-C id is imported as Any, and you cannot call any operations for Any including ===, without explicit cast.

试试这个:

if sender as AnyObject? === saveButton {

(其他sender 比较也一样.)

(All the same for other sender comparison.)

请记住,在 Swift 3 中,as AnyObject 已成为最危险的操作之一,您不应该在其他情况下使用 as AnyObject.

And remember, in Swift 3, as AnyObject has become one of the most risky operations, you should not use as AnyObject in other cases.

这篇关于二元运算符“==="不能应用于“Any?"类型的操作数和“UIBarButtonItem!"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆