Swift - what's the difference between metatype .Type
.Type
Under the hood? Basically where ever you see? If you are confused as to what does ‘Swift.‘ do then above,then see the comments from?here The above could have also been written as: .self
Where is it used? If you are writing/creating a function that accepts a?type?e.g. class,?not?an instance then to you would write? In continuation of the tableView code: Playground code: ? Easy example struct Something { ? ? var x = 5 } ? let a = Something() type(of:a) == Something.self // true Hard example class SomeBaseClass { ? ? class func printClassName() { ? ? ? ? print("SomeBaseClass") ? ? } } class SomeSubClass: SomeBaseClass { ? ? override class func printClassName() { ? ? ? ? print("SomeSubClass") ? ? } } ? ? let someInstance: SomeBaseClass = SomeSubClass() /*? ? ? ? ? ? ? ? ? ? ? |? ? ? ? ? ? ? ? | ? ? ? ? ? ? ? ? ? ? compileTime ? ? ? Runtime ? ? ? ? ? ? ? ? ? ? ? ? |? ? ? ? ? ? ? ? |? To extract,use: ? ? ? .self? ? ? ? ? type(of) ? ? The compile-time type of someInstance is SomeBaseClass, ? and the runtime type of someInstance is SomeSubClass */ ? type(of: someInstance) == SomeSubClass.self // TRUE type(of: someInstance) == SomeBaseClass.self // FALSE I highly recommend to read?Apple documentation on Types. Also see?here ? https://stackoverflow.com/questions/31438368/swift-whats-the-difference-between-metatype-type-and-self (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |