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

查看:98
本文介绍了二进制运算符'==='不能应用于类型为'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比较均相同.)

请记住,在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天全站免登陆