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

前端之家收集整理的这篇文章主要介绍了如何在swift中检查变量的类型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道我稍后初始化的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.

原文链接:https://www.f2er.com/swift/318660.html

猜你在找的Swift相关文章