博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Swift - what's the difference between metatype .Type and .self?
阅读量:7196 次
发布时间:2019-06-29

本文共 2327 字,大约阅读时间需要 7 分钟。

 

Declaration

typealias AnyClass = .Type

.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 : 

 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 

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 : 

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 . Also see 

 

https://stackoverflow.com/questions/31438368/swift-whats-the-difference-between-metatype-type-and-self

转载地址:http://jatkm.baihongyu.com/

你可能感兴趣的文章
參考mudo logging写的win下logging
查看>>
云数据库PolarDB(一)
查看>>
[数据结构] 迷宫问题(栈和队列,深搜和广搜)
查看>>
找不到对象?也许你应该这样做
查看>>
Hadoop集群动态服役新的数据节点&&退役数据节点
查看>>
p4137 Rmq Problem / mex
查看>>
python学习之路---day16--面向对象
查看>>
打造一个高逼格的android开源项目——小白全攻略 (转)
查看>>
JavaScript 基础学习(二)
查看>>
Linux 之Shell for循环
查看>>
多线程交互
查看>>
for循环里面的break;和continue;语句
查看>>
CSS Sprites技术原理和使用
查看>>
追踪电子表格中的单元格
查看>>
ScrollView嵌套ViewPager,ViewPager内容不显示问题
查看>>
运行微信支付demo
查看>>
启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法!
查看>>
springMVC中不通过注解方式获取指定Service的javabean
查看>>
Kruskal算法(求最小生成树)
查看>>
JavaScript-事件周期-点击替换颜色
查看>>