Swift - what's the difference between metatype .Type and .self?
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
isSomeClass.Type
and the metatype of the protocolSomeProtocol
isSomeProtocol.Protocol
.From Apple : metaType Type
Under the hood AnyClass
is
Basically where ever you see AnyClass
, Any.Type
, AnyObject.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.self
, CustomTableView.self
, someOtherClass.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 学习(二)基础知识 (函数,闭包,ARC,柯里化,反射)
函数 func x(a:Int, b:Int) {} func x(a:Int, b:Int) -> Void {} func x(a:Int, b:Int) ->(Int,Int ...
- Swift和C#的基本语法对比
Recently, Apple announced and released a beta version of the new Swift programming language for buil ...
- Swift中的反射
原文:http://www.cocoachina.com/applenews/devnews/2014/0623/8923.html Swift 事实上是支持反射的.只是功能略弱. 本文介绍主要的反射 ...
- Swift: Associated Types--为什么协议使用关联类型而不是泛型
关联类型的形式为类型的引用进而进行约束提供了条件: 同时能够简化语法形式. Swift: Associated Types http://www.russbishop.net/swift-associ ...
- Swift 语言附注 类型
本页包括内容: 类型注解(Type Annotation) 类型标识符(Type Identifier) 元组类型(Tuple Type) 函数类型(Function Type) 数组类型(Array ...
- Swift语法3.03(类型Types)
类型 在Swift中,有两种类型:命名型类型和复合型类型.命名型类型是在定义时可以给定的特定名字的类型.命名型类型包括类,结构体,枚举和协议.例如,自定义的类MyClass的实例拥有类型MyClass ...
- input type="submit" 和"button"有什么区别?
http://www.zhihu.com/question/20839977 在一个页面上画一个按钮,有四种办法: <input type="button" /> 这就 ...
- swift中文文档翻译之--字符串和字符
字符串和字符 A string is an ordered collection of characters, such as "hello, world" or "al ...
- iOS - Swift 与 C 语言交互编程
前言 作为一种可与 Objective-C 相互调用的语言,Swift 也具有一些与 C 语言的类型和特性,如果你的代码有需要,Swift 也提供了和常见的 C 代码结构混合编程的编程方式. 1.基本 ...
随机推荐
- python3使用465端口发送邮件来解决阿里云封闭25端口问题
import smtplibfrom email.mime.text import MIMETextfrom email.utils import formataddr #发件人邮箱账号my_send ...
- HDU1029 - Ignatius and the Princess IV【水题】
给你n个数字,请你找出出现至少(n+1)/2次的数字. 输入 本题包含多组数据,请处理到EOF: 每组数据包含两行. 第一行一个数字N(1<=N<=999999) ,保证N为奇数. 第二行 ...
- nyoj24-素数 距离问题
素数距离问题 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度.如果左右有等距离长度素数 ...
- SSH(远程登录)
在linux中SSH服务对应两个配置文件: ssh特点:在传输数据的时候,对文件加密后传输. ssh作用:为远程登录会话和其他网络服务提供安全性协议. ssh小结: 1.SSH是安全的加密协议,用于远 ...
- 【hihocoder 1329】平衡树·Splay(Splay做法)
[题目链接]:http://hihocoder.com/problemset/problem/1329 [题意] [题解] 插入操作:-,记住每次插入之后都要把它放到根节点去就好; 询问操作:对于询问 ...
- MySQL特异功能之:Impossible WHERE noticed after reading const tables
用EXPLAIN看MySQL的执行计划时经常会看到Impossible WHERE noticed after reading const tables这句话,意思是说MySQL通过读取"c ...
- Java 中 synchronized的用法详解
Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 1.方法声明时使用,放在范围操作符(public等)之后,返回类型声明(void等)之 ...
- C#--进程-线程
线程 线程也被称为轻量级进程lightweight process ,LWP,线程是CPU独立调度和分派的基本单位,同一个进程中的多个线程将共享该进程中的全部系统资源,多线程共享堆heap资源,c#程 ...
- Elasticsearch顶尖高手系列课程推荐
Elasticsearch,是目前行业中非常热门的一个技术.Elasticsearch是一种分布式的海量数据搜索与分析的技术,可以用于电商网站.门户网站.企业IT系统等各种场景下的搜索引擎,也可以用于 ...
- javascript的函数、创建对象、封装、属性和方法、继承
转自原文javascript的函数.创建对象.封装.属性和方法.继承 一,function 从一开始接触到js就感觉好灵活,每个人的写法都不一样,比如一个function就有N种写法 如:functi ...