gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 05:06:04 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com