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. python3使用465端口发送邮件来解决阿里云封闭25端口问题

    import smtplibfrom email.mime.text import MIMETextfrom email.utils import formataddr #发件人邮箱账号my_send ...

  2. 今天写了一个简单的新浪新闻RSS操作类库

    今天,有位群友问我如何获新浪新闻列表相关问题,我想,用正则表达式网页中取显然既复杂又不一定准确,现在许多大型网站都有RSS集合,所以我就跟他说用RSS应该好办一些. 一年前我写过一个RSS阅读器,不过 ...

  3. 用户输入input函数和代码注释

    一.读取用户输入 py3中input()读取用户输入,输出全部是默认str字符串数据类型,一般将其赋值变量,用户输入才继续往下走程序.(py2的不同已单独列出随笔) 二.注释 注释的作用:代码量大的时 ...

  4. PHP学习总结(11)——PHP入门篇之WAMPServer多站点配置

  5. asp.net--owin的知识点

    一篇很好的介绍owin的文章 http://kb.cnblogs.com/page/509236/ Owin在webconfig中定义启动配置类IdentityConfig的方法(代码来自极客学院的教 ...

  6. [bzoj4084][Sdoi2015]双旋转字符串_hash

    双旋转字符串 bzoj-4084 Sdoi-2015 题目大意:给定两个字符串集合 S 和 T .其中 S 中的所有字符串长度都恰好为 N ,而 T 中所有字符串长度都恰好为 M .且 N+M 恰好为 ...

  7. 系统报 “client没有所需的特权” 的解决方法

    今在对服务端代码进行单元測试的时候.突然报出例如以下错误: client没有所需的特权 后经网上查找,相同的问题都是属于对C盘读写的问题.回忆自己的项目也须要对C盘进行创建文件夹和读写文件.故尝试运行 ...

  8. 赵雅智_ListView_SimpleAdapter

    项目步骤 声明listView控件并获取显示的视图 获取显示的数据 设置显示的adapter 注冊点击事件 详细案例 实现效果: 查找的方法 public List<Map<String, ...

  9. 安装多个版本号jdk后java -version不改变的问题解决

    1.问题 比方先安装了jdk7后,再安装jdk6,java -version就是显示java1.6,即使把JAVA_HOME和path改动为java7的路径,java -version依旧还是显示6. ...

  10. 多人即时战斗游戏服务端系列[2]--90坦克Online游戏对象介绍以及渲染机制

    先上类图,略大,点击此处放大: 1.先说下方接口 1.1 场景物品接口 ISceneObject : OpLog.IOpItem, IStackPoolObject 全部场景对象的基本接口,包含类型定 ...