如何在 Swift 中将元组转换为 AnyObject [英] How to convert Tuple to AnyObject in Swift

查看:40
本文介绍了如何在 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.

这篇关于如何在 Swift 中将元组转换为 AnyObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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