如何在 Swift 中将元组转换为 AnyObject
问题描述:
以下代码编译出错:Error:(112, 20) type '(String, Int)' 不符合协议 'AnyObject'
func myMethode() {
aMethodeThatICanNotChange {
let a = ("John",7)
return a // Error:(112, 20) type '(String, Int)' does not conform to protocol 'AnyObject'
}
}
func aMethodeThatICanNotChange(closure: () -> AnyObject) {
// do something with closure
}
如何将元组转换/转换为 AnyObject?
How can I cast/convert a Tuple to AnyObject?
答
如何将元组转换/转换为 AnyObject?
How can I cast/convert a Tuple to AnyObject?
简单地说,你不能.只有类类型(或桥接到基础类类型的类型)可以转换为 AnyObject.元组不是类类型.
Simply put, you can't. Only class types (or types that are bridged to Foundation class types) can be cast to AnyObject. A tuple is not a class type.
当然,可能还有很多其他方法可以完成您真正想要完成的任务.但至于你的特定问题,你做不到.
Of course there may be lots of other ways to accomplish whatever you're really trying to accomplish. But as for your particular question, you can't do it.