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语言里的数 ...
随机推荐
- 解决JS中missing ( before function parameters的错误
在编写javascript中,常出现在function处提示“missing ( before function parameters”的错误,这是怎么回事? 例如: function String. ...
- C# 如何操作串口
1.首先要引用 System.IO.Ports using System; using System.Collections.Generic; using System.ComponentModel ...
- 2018.09.23 bzoj1076: [SCOI2008]奖励关(期望+状压dp)
传送门 一道神奇的期望状压dp. 用f[i][j]f[i][j]f[i][j]表示目前在第i轮已选取物品状态为j,从现在到第k轮能得到的最大贡献. 如果我们从前向后推有可能会遇到不合法的情况. 所以我 ...
- linux安装mysql~~~mysql5.6.12
Linux安装mysql服务器 准备: MySQL-client-5.6.12-1.rhel5.i386.rpm MySQL-server-5.6.12-1.rhel5.i386.rpm 首先检查环境 ...
- Sql Server R8 密码问题及5102错误
登录的两种方式: 集成登录连接:con = new SqlConnection("server=.\\SQLEXPRESS;database=db_news;Trusted_Connecti ...
- *C语言的小技巧
计算数组长度 ,,,,}; int Length=sizeof(a)/sizeof(int); 交换a和b的值,不借用辅助变量 a=a+b; b=a-b; a=a-b; 将0-9的字符转化为整数 '; ...
- Hdu2612 Find a way 2017-01-18 14:52 59人阅读 评论(0) 收藏
Find a way Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Su ...
- 105032014138_牟平_z作业1
2)NextDate函数问题 NextDate函数说明一种复杂的关系,即输入变量之间逻辑关系的复杂性 NextDate函数包含三个变量month.day和year,函数的输出为输入日期后一天的日期. ...
- fully delete project in Eclipse
选择你的项目(test)右击,选择delete——弹出框中勾选删除全部,如下如所示: 正常情况下,这样就能删除干净了,有时候你项目在运行,这时候你点击删除,那就会报下面的错误提示,虽然不会影响你其它项 ...
- Create Your Content and Structure
The original page source Content is the most important aspect of any site. So let's design for the c ...