Swift 里集合类型协议的关系


Sequence
A type that provides sequential, iterated access to its elements.
是最基础的协议,可以通过迭代来获取它的元素。
有两个关联类型:
/// A type representing the sequence's elements.
associatedtype Element
/// A type that provides the sequence's iteration interface and
/// encapsulates its iteration state.
associatedtype Iterator : IteratorProtocol where Iterator.Element == Element
遵守Sequence 协议,只需要实现makeIterator()方法即可。
在迭代过程中,Sequence的序列是否被破坏,并没有要求。因此,两次迭代同一个Sequence,可能会得到不同的结果。
Collection
A sequence whose elements can be traversed multiple times, nondestructively, and accessed by an indexed subscript.
在sequence基础上,增加了几个性质
- 多次遍历,不会对序列的结构造成影响
- 可以通过下标索引
引入了几个和索引相关的关联类型
associatedtype Index : Comparable
/// A type that represents the indices that are valid for subscripting the
/// collection, in ascending order.
associatedtype Indices : Collection = DefaultIndices<Self>
where Indices.Element == Index,
Indices.Index == Index,
Indices.SubSequence == Indices
通过下标获取值的方法如下,复杂度是O(1)
subscript(position: Index) -> Element { get }
可以通过下标来遍历,那么必须定义下标的successor
func index(after i: Self.Index) -> Self.Index
BidirectionalCollection
A collection that supports backward as well as forward traversal.
继承了Collection协议,增加了可以反向遍历的功能。
通过一个下标,可以找到上一个下标。
func index(before i: Index) -> Index
这也为和last相关的方法提供了基础
RandomAccessCollection
A collection that supports efficient random-access index traversal.
继承了BidirectionalCollection,因此可以正向/反向遍历。还对遍历的效率做出来要求,
Random-access collections can move indices any distance and measure the distance between indices in O(1) time
MutableCollection
A collection that supports subscript assignment.
继承了collection协议,提供了可以改变元素值的能力。
override subscript(position: Index) -> Element { get set }
这个下标操作,提供了set方法。
对应的,提供了默认的分片方法(O(n))
/// - Complexity: O(*n*), where *n* is the length of the collection.
mutating func partition(
by belongsInSecondPartition: (Element) throws -> Bool
) rethrows -> Index
也提供了交换方法
/// - Complexity: O(1)
mutating func swapAt(_ i: Index, _ j: Index)
RangeReplaceableCollection
A collection that supports replacement of an arbitrary subrange of elements with the elements of another collection.
继承了collection,增加了可以某一个子区间内的元素,可以被另一个collection替代的能力。
为collection增加了插入和删除的能力。
mutating func append(_ newElement: __owned Element)
mutating func append<S : Sequence>(contentsOf newElements: __owned S)
where S.Element == Element
mutating func insert(_ newElement: __owned Element, at i: Index)
mutating func insert<S : Collection>(contentsOf newElements: __owned S, at i: Index)
where S.Element == Element
mutating func remove(at i: Index) -> Element
mutating func removeSubrange(_ bounds: Range<Index>)
Swift 里集合类型协议的关系的更多相关文章
- Java中的集合类型的继承关系图
Java中的集合类型的继承关系图
- [Swift]遍历集合类型(数组、集合和字典)
Swift提供了三种主要的集合类型,称为数组,集合和字典,用于存储值集合. 数组是有序的值集合. 集是唯一值的无序集合. 字典是键值关联的无序集合. Swift中无法再使用传统形式的for循环. // ...
- Swift 进阶 第 4 课 集合类型协议
• Read 序列 一个序列 (sequence) 代表的是一系列具有相同类型 的值,你可以对这些值进行迭代.遍历一个序列最简单的方式是使用 for 循环: 123 for element in so ...
- Swift中集合类型indexOf(Element)提示错误的解决办法
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 初学Swift,会遇到一些潜在的小问题,比如我们在某个集合对象 ...
- Swift中的集合类型
一.引子: 在2014年10月TIOBE编程语言排行榜中,Swift位居第18位,从2014WWDC发布会首次公布至今不到半年时间,swift一直受到编程人 员的追捧,其热衷程度并不亚于当红巨星Tay ...
- swift 集合类型(二)
说到swift的集合类型,就不得不谈到Dictionary.包含一个键值对组合的集合. var air = ["name":"warner","tit ...
- Swift 集合类型
Swift语言提供数组和字典的集合类型 Swift 语言里的数组和字典中存储的数据值类型必须明确 ,即数组中只能存放同类型的数据. 1: 数组 数组的创建 var shoppingList: St ...
- Swift的基础,操作符,字符串和集合类型
这篇文章主要讲解苹果Swift官方指南的第二章前四节的要点内容,如果想看完整的英文文档可以去苹果开发者页面下载. Basic 声明常量let 声明变量var 注释依旧使用"//" ...
- The Swift Programming Language-官方教程精译Swift(5)集合类型 -- Collection Types
Swift语言提供经典的数组和字典两种集合类型来存储集合数据.数组用来按顺序存储相同类型的数据.字典虽然无序存储相同类型数据值但是需要由独有的标识符引用和寻址(就是键值对). Swift语言里的数 ...
随机推荐
- 属性表格 datagridproperty
http://www.cnblogs.com/yxlblogs/p/3468921.html
- PHP 用 ZipArchive 打包指定文件到zip供用户下载
Ubuntu需安装zlib sudo apt-get install ruby sudo apt-get install zlib1g zlib1g.dev Windows需开启php_zip.d ...
- 20155207 2016-2017-2 《Java程序设计》第九周学习总结
20155207 2016-2017-2 <Java程序设计>第九周学习总结 教材学习内容总结 第16章 整合数据库 16.1 JDBC入门 16.1.1 JDBC简介 数据库本身是个独立 ...
- Nginx upstream的5种权重分配方式(转)
出处:http://www.cnblogs.com/funsion/p/4003499.html?utm_source=tuicool 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器, ...
- Matlab 中以分数显示结果
转http://www.blogbus.com/shijuanfeng-logs/234881647.html Matlab,计算得到的结果一般是小数形式. 但为了更精确表示,我们有时候需要用到分数形 ...
- faceswap使用手冊
cd faceswap cd faceswap-master python faceswap.py gui Notice: This repository is not operated or mai ...
- (字典树模板)统计难题--hdu--1251
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 在自己敲了一遍后终于懂了,这不就用了链表的知识来建立了树,对!就是这样的,然后再查找 代码: #i ...
- paip.双网卡多网卡不能上网的联网配置
paip.双网卡多网卡不能上网的联网配置 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/att ...
- java实现WC项目
个人项目:WC wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个命令行程序,模仿已有wc.exe 的功能,并加以扩充,给出某程序设计语言源文件的字符数.单 ...
- Delphi Dll 动态调用例子(2)
http://zhidao.baidu.com/question/157196792.html delphi动态调用DLL 写了个1.dll内容如下 library Project2; uses Sy ...