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

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

发布时间:2020-12-14 04:27:51 所属栏目:百科 来源:网络整理
导读:参见英文答案 Swift dynamic type checking for structs?????????????????????????????????????2个 Swift 3中的type(of:x)似乎不适用于is类型检查 它只是给出了此错误消息: Consecutive statements on a line must be separated by ‘;’ 代码示例: class
参见英文答案 > Swift dynamic type checking for structs?????????????????????????????????????2个
Swift 3中的type(of:x)似乎不适用于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中进行这样的动态类型检查?

解决方法

在这个特定的背景下:

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)

将返回true,但仅当它是同一个类而不是子类时

(编辑:李大同)

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

    推荐文章
      热点阅读