加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在swift中检查变量的类型

发布时间:2020-12-14 05:39:17 所属栏目:百科 来源:网络整理
导读:我想知道我稍后初始化的AnyObject变量的类型是什么. 例如: var test: AnyObjecttest = 12.2 我无法弄清楚如何做到这一点. 您可以使用is运算符执行此操作. 示例代码: var test: AnyObjecttest = 12.2if test is Double { println("Double type")} else if t
我想知道我稍后初始化的AnyObject变量的类型是什么.
例如:
var test: AnyObject

test = 12.2

我无法弄清楚如何做到这一点.

您可以使用is运算符执行此操作.
示例代码:
var test: AnyObject

test = 12.2

if test is Double {
    println("Double type")
} else if test is Int {
    println("Int type")
} else if test is Float {
    println("Float type")
} else {
    println("Unkown type")
}

根据Apple文档:

Checking Type

Use the type check operator (is) to check whether an instance is of a certain subclass type. The type check operator returns true if the instance is of that subclass type and false if it is not.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读