I want to know what the type is of a AnyObject variable that i initialise later. For example:
我想知道我稍后初始化的AnyObject变量的类型是什么。例如:
var test: AnyObject
test = 12.2
I cant figure out how to do this.
我无法弄清楚如何做到这一点。
1 个解决方案
#1
8
You can do this with the is
operator. Example code:
您可以使用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")
}
According to Apple docs:
根据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.
使用类型检查运算符(is)检查实例是否属于某个子类类型。如果实例属于该子类类型,则类型检查运算符返回true,否则返回false。
#1
8
You can do this with the is
operator. Example code:
您可以使用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")
}
According to Apple docs:
根据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.
使用类型检查运算符(is)检查实例是否属于某个子类类型。如果实例属于该子类类型,则类型检查运算符返回true,否则返回false。