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.基本 ...
随机推荐
- python中字节与字符串的转换
#bytes object byte = b"byte example" # str object str = "str example" ...
- HTML学习笔记之基本介绍
超文本标记语言 (Hyper Text Markup Language,HTML)不是一种编程语言,而是一种标记语言,用一套标记标签描述网页 HTML 标记标签又被称为 HTML 标签(HTML Ta ...
- Codeforces 805D/804B - Minimum number of steps
传送门:http://codeforces.com/contest/805/problem/D 对于一个由‘a’.‘b’组成的字符串,有如下操作:将字符串中的一个子串“ab”替换成“bba”.当字符串 ...
- fzoj 2113数位dp
参考http://blog.csdn.net/xingyeyongheng/article/details/8785785 #include<stdio.h> #define ll lon ...
- Solr部分更新MultiValued的Date日期字段时报错及解决方案:Invalid Date String:'Mon Sep 14 01:48:38 CST 2015'
问题描述如标题. 异常信息如下: Result Caused by: org.apache.solr.common.SolrException: Invalid Date String:'Mon Se ...
- 如何实现网卡bond
https://jingyan.baidu.com/article/375c8e19da666325f2a229f7.html
- [bzoj2631]tree_LCT
tree bzoj-2631 题目大意:给定一个n个点的树,每个点的初始权值为1,支持:删边加边(这两个操作同时进行,保证操作之后还是一棵树),路径加,路径乘,查询路径和. 注释:$1\le n,q\ ...
- [bzoj2600][Ioi2011]ricehub_二分
ricehub bzoj-2600 Ioi-2011 题目大意:在数轴上有r块稻田,稻田坐标为整数.计划建造一个米仓,使得它可以收取尽量多的稻米.米仓的坐标仍需为整数.每一块权值为val的稻田距离米仓 ...
- 一次获取多个oracle序列值
一次获取多个oracle序列值 学习了:http://blog.csdn.net/wangchsh2008/article/details/53495961 select seq_one.nextva ...
- spring学习笔记(22)声明式事务配置,readOnly无效写无异常
在上一节内容中.我们使用了编程式方法来配置事务,这种优点是我们对每一个方法的控制性非常强.比方我须要用到什么事务,在什么位置假设出现异常须要回滚等.能够进行非常细粒度的配置.但在实际开发中.我们可能并 ...