Swift 3 中的 type(of: ) 似乎不适用于 is 类型检查

type(of: ) in Swift 3 does not seem to work with the is type check

type(of: x) in Swift 3 似乎不适用于 is 类型检查

它只是给出了这个错误信息:

Consecutive statements on a line must be separated by ';'

代码示例:

class Test : UIViewController {}

let test = Test()
let test2 = Test()
let isEqual = test2 is type(of: test) // this does not compile

这里有什么问题?
如何在 Swift 3 中进行动态类型检查?

您正在比较 2 种不同的无法比较的类型,除非您重载 == 来处理此类

,否则它们将无法工作

test2 对比 type(of: test)


正如康纳评论的那样:

print(type(of: test)) // Test
print(test2) // <__lldb_expr_22.Test: 0x7ffe8cb063e0>

let isEqual = type(of:test2) == type(of: test) // true

在此特定上下文中:

class Test : UIViewController {}

class Test1 : UIViewController {}

let test = Test()
let test2 = Test1()

let f = test2.isKind(of: test2.classForCoder)

print(f)

Output: true

这可能是检查类型的另一个想法

除此之外

let q = type(of: test) == type(of: test)

将 return 为真,但前提是相同 class 不是子class