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. 洛谷P1002 过河卒【dp】

    棋盘上AA点有一个过河卒,需要走到目标BB点.卒行走的规则:可以向下.或者向右.同时在棋盘上CC点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为"马拦过河卒 ...

  2. BZOJ 3744 Gty的妹子序列 (分块+树状数组+主席树)

    题面传送门 题目大意:给你一个序列,多次询问,每次取出一段连续的子序列$[l,r]$,询问这段子序列的逆序对个数,强制在线 很熟悉的分块套路啊,和很多可持久化01Trie的题目类似,用分块预处理出贡献 ...

  3. SQLServer · BUG分析 · Agent 链接泄露分析(转载)

    背景 SQLServer Agent作为Windows服务提供给用户定期执行管理任务,这些任务被称为Job:考虑应用镜像的场景如何解决Job同步问题,AWS RDS的做法是不予理会,由用户维护Job, ...

  4. Codeforces 879A/B

    A. Borya's Diagnosis 传送门:http://codeforces.com/contest/879/problem/A 本题是一个模拟问题. 依次访问n个元素,第i个元素首次出现于s ...

  5. mongodb--入门知识点

    命令 mongo.exe,命令行客户端Shell工具. mongod.exe,数据库服务程序. mongodump.exe,数据库备份程序. mongoexport.exe,数据导出工具. mongo ...

  6. SpringBoot支持AJAX跨域请求

    利用注解的方式解决AJAX请求跨域问题 1.编写一个支持跨域请求的 Configuration - 第一种方式 - CorsConfig.java import org.springframework ...

  7. eclipse重置页面恢复到最初布局状态

    eclipse重置页面恢复到最初布局状态 window->perspective->reset perspective

  8. Ruby 教程

    Ruby 教程 http://www.runoob.com/ruby/ruby-tutorial.html http://guides.rubyonrails.org/getting_started. ...

  9. 杭电(hdu)ACM 1010 Tempter of the Bone

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  10. [Javascript] IntersectionObserver -- Lazy Load Images on a Website

    When it comes to websites performance is king. How long it takes for a page to load can mean the dif ...