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

Swift - what's the difference between metatype .Type

发布时间:2020-12-14 05:12:34 所属栏目:百科 来源:网络整理
导读:.Type The metatype of a class,structure,or enumeration type is the name of that type followed by .Type. The metatype of a protocol type—not the concrete type that conforms to the protocol at runtime—is the name of that protocol followed

.Type

The metatype of a class,structure,or enumeration type is the name of that type followed by .Type. The metatype of a protocol type—not the concrete type that conforms to the protocol at runtime—is the name of that protocol followed by .Protocol. For example,the metatype of theclass?type?SomeClass?is?SomeClass.Type?and the metatype of the?protocol?SomeProtocol?is?SomeProtocol.Protocol.

From Apple :?metaType Type

Under the hood?AnyClass?is

 

Basically where ever you see?AnyClass,?Any.Type,?AnyObject.Type,its because it‘s in need of a type. A very very common place we see it is when we want to?register?a class for our tableView using?register?func.

 

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

You can use the?postfix?self expression to access a type as a value. For example,SomeClass.self returns SomeClass itself,?not?an instance of SomeClass. And SomeProtocol.self returns SomeProtocol itself,?not?an instance of a type that conforms to SomeProtocol at runtime. You can use a type(of:) expression with an instance of a type to access that instance’s dynamic,runtime type as a value,as the following example shows:

From Apple :?metaType Type

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?T.Type?as the type of the parameter. What it expects as a parameter can be:?String.self,?CustomTableView.self,?someOtherClass.self.

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

(编辑:李大同)

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

    推荐文章
      热点阅读