Declaration

typealias AnyClass = AnyObject.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 : 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?的更多相关文章

  1. swift 学习(二)基础知识 (函数,闭包,ARC,柯里化,反射)

    函数 func x(a:Int, b:Int)  {}   func x(a:Int, b:Int) -> Void {}  func x(a:Int, b:Int) ->(Int,Int ...

  2. Swift和C#的基本语法对比

    Recently, Apple announced and released a beta version of the new Swift programming language for buil ...

  3. Swift中的反射

    原文:http://www.cocoachina.com/applenews/devnews/2014/0623/8923.html Swift 事实上是支持反射的.只是功能略弱. 本文介绍主要的反射 ...

  4. Swift: Associated Types--为什么协议使用关联类型而不是泛型

    关联类型的形式为类型的引用进而进行约束提供了条件: 同时能够简化语法形式. Swift: Associated Types http://www.russbishop.net/swift-associ ...

  5. Swift 语言附注 类型

    本页包括内容: 类型注解(Type Annotation) 类型标识符(Type Identifier) 元组类型(Tuple Type) 函数类型(Function Type) 数组类型(Array ...

  6. Swift语法3.03(类型Types)

    类型 在Swift中,有两种类型:命名型类型和复合型类型.命名型类型是在定义时可以给定的特定名字的类型.命名型类型包括类,结构体,枚举和协议.例如,自定义的类MyClass的实例拥有类型MyClass ...

  7. input type="submit" 和"button"有什么区别?

    http://www.zhihu.com/question/20839977 在一个页面上画一个按钮,有四种办法: <input type="button" /> 这就 ...

  8. swift中文文档翻译之--字符串和字符

    字符串和字符 A string is an ordered collection of characters, such as "hello, world" or "al ...

  9. iOS - Swift 与 C 语言交互编程

    前言 作为一种可与 Objective-C 相互调用的语言,Swift 也具有一些与 C 语言的类型和特性,如果你的代码有需要,Swift 也提供了和常见的 C 代码结构混合编程的编程方式. 1.基本 ...

随机推荐

  1. 51nod1185 威佐夫游戏 V2【博弈论】

    有2堆石子.A B两个人轮流拿,A先拿.每次可以从一堆中取任意个或从2堆中取相同数量的石子,但不可不取.拿到最后1颗石子的人获胜.假设A B都非常聪明,拿石子的过程中不会出现失误.给出2堆石子的数量, ...

  2. Mybatis配置之别名配置元素详述

    这里我们贴出之前的UserDao对应的mapper文件,如下所示 从这个配置文件中,我们可以看到<select>.<insert>和<update>三个标签元素的r ...

  3. python类中属性逗号引发的类型改变

    不注意点了个逗号引发了类型改变 [shangbl@newsvn ~]$ cat test.py class AB1: a="a" class AB12: a="a&quo ...

  4. noip模板复习

    自己敲模板还是有很多容易错的地方 写在注释里面了 LCA #include<bits/stdc++.h> #define REP(i, a, b) for(register int i = ...

  5. 【例题 4-5 uva 512】Spreadsheet Tracking

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个操作对与一个点来说变化是固定的. 因此可以不用对整个数组进行操作. 对于每个询问,遍历所有的操作.对输入的(x,y)进行相应的变 ...

  6. Spring Cloud-Zuul(十)

    个人理解 在微服务体系体系中 我们会有很多服务.在内部体系中 通过eureka实现服务的自动发现通过ribbon实现服务的调用.但是如果对外部体系提供接口 我们就会涉及到接口的安全性,我们不能可能对每 ...

  7. navicat 为表添加索引

    navicat 为表添加索引 分析常用的查询场景,为字段添加索引,增加查询速度. 可以添加单列索引,可以添加联合索引. 右键,设计表中可以查看和添加修改索引! 索引一定要根据常用的查询场景进行添加! ...

  8. CSTC 选课

    选课 [问题描述] 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门课有个学分, ...

  9. 简陋版:基于python的自动化测试框架开发

    项目背景: XXXX银行项目采用的是B/S架构,主要是为了解决银行业务中的柜员.凭证.现金.账务等来自存款.贷款.会计模块的管理. 手工弊端: 1.项目业务复杂度高,回归测试工作量大2.单个接口功能比 ...

  10. HTML5和CSS3中的交互新特性

    当文章的标题是一副用photoshop制作的图片,那么势必在搜索引擎中无法搜索到.并且因为图片的体积不算小.可能在网速慢的的时候不得不耐心的等待图片的刷新. 所以.我们来谈谈有没有一种新的方法能够避免 ...