在Swift有什么相当的东西吗?
在文档中可以找到最接近的东西是使用is <Class>
keyword进行“类型检查”的能力,但这只能帮助我猜一下。
我已经遇到了一个few situations,在那里我认为我有一种类型的课,但实际上有另一种,不知道如何知道肯定。
var label = UILabel() println(label.dynamicType.description()) println(label.isKindOfClass(UILabel.self))
斯威夫特-3
var label = UILabel() println(type(of: label).description())
Output
UILabel
true
这里有更多的背景 – 有两个表达式要注意:后缀自我表达式和动态类型表达式。从the docs:
Postfix Self
A postfix self expression consists of an expression or the name of a
type,immediately followed by .self. It has the following forms:06002
The first form evaluates to the value of the expression. For example,
x.self evaluates to x.The second form evaluates to the value of the type. Use this form to
access a type as a value. For example,because SomeClass.self
evaluates to the SomeClass type itself,you can pass it to a function
or method that accepts a type-level argumentDyamic Type Expression
A dynamicType expression consists of an expression,immediately
followed by .dynamicType. It has the following form:06003
The expression can’t be the name of a type. The entire dynamicType expression evaluates to the value of the runtime type of the expression.