Swift - what's the difference between metatype .Type and .self?

Swift - what's the difference between metatype .Type and .self?,第1张

概述.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 .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 AnyClassAny.TypeAnyObject.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.selfCustomtableVIEw.selfsomeOtherClass.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

总结

以上是内存溢出为你收集整理的Swift - what's the difference between metatype .Type and .self?全部内容,希望文章能够帮你解决Swift - what's the difference between metatype .Type and .self?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1024319.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存