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基础上,增加了几个性质

  1. 多次遍历,不会对序列的结构造成影响
  2. 可以通过下标索引

引入了几个和索引相关的关联类型

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 里集合类型协议的关系的更多相关文章

  1. Java中的集合类型的继承关系图

    Java中的集合类型的继承关系图

  2. [Swift]遍历集合类型(数组、集合和字典)

    Swift提供了三种主要的集合类型,称为数组,集合和字典,用于存储值集合. 数组是有序的值集合. 集是唯一值的无序集合. 字典是键值关联的无序集合. Swift中无法再使用传统形式的for循环. // ...

  3. Swift 进阶 第 4 课 集合类型协议

    • Read 序列 一个序列 (sequence) 代表的是一系列具有相同类型 的值,你可以对这些值进行迭代.遍历一个序列最简单的方式是使用 for 循环: 123 for element in so ...

  4. Swift中集合类型indexOf(Element)提示错误的解决办法

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 初学Swift,会遇到一些潜在的小问题,比如我们在某个集合对象 ...

  5. Swift中的集合类型

    一.引子: 在2014年10月TIOBE编程语言排行榜中,Swift位居第18位,从2014WWDC发布会首次公布至今不到半年时间,swift一直受到编程人 员的追捧,其热衷程度并不亚于当红巨星Tay ...

  6. swift 集合类型(二)

    说到swift的集合类型,就不得不谈到Dictionary.包含一个键值对组合的集合. var air = ["name":"warner","tit ...

  7. Swift 集合类型

     Swift语言提供数组和字典的集合类型  Swift 语言里的数组和字典中存储的数据值类型必须明确 ,即数组中只能存放同类型的数据. 1: 数组 数组的创建 var shoppingList: St ...

  8. Swift的基础,操作符,字符串和集合类型

    这篇文章主要讲解苹果Swift官方指南的第二章前四节的要点内容,如果想看完整的英文文档可以去苹果开发者页面下载. Basic 声明常量let 声明变量var 注释依旧使用"//" ...

  9. The Swift Programming Language-官方教程精译Swift(5)集合类型 -- Collection Types

    Swift语言提供经典的数组和字典两种集合类型来存储集合数据.数组用来按顺序存储相同类型的数据.字典虽然无序存储相同类型数据值但是需要由独有的标识符引用和寻址(就是键值对).   Swift语言里的数 ...

随机推荐

  1. eclipse 远程调试mapreduce

    使用环境:centos6.5+eclipse(4.4.2)+hadoop2.7.0 1.下载eclipse hadoop 插件  hadoop-eclipse-plugin-2.7.0.jar 粘贴到 ...

  2. 2018.09.10 loj#10172. 涂抹果酱(状压dp)

    传送门 三进制状压感觉有点难写啊. 不过这题状态转移方程挺简单的. 就直接f[i][j]表示前i行第i行状态为j时的选法总数,分情况转移就行了. 代码: #include<bits/stdc++ ...

  3. python3.4用循环往mysql5.7中写数据并输出

    #!/usr/bin/env python # -*- coding:utf-8 -*- # __author__ = "blzhu" """ pyt ...

  4. python 文件合并和编号

    # -*- coding:utf-8 -*- import os import re p1=r"([0-9][0-9][AB])\.\w{3}$" pattern1=re.comp ...

  5. 简单的nginx模拟网站的负载均衡

    环境:nginx1.10.3 虚拟机环境:3台centos7虚拟机 将148机器作为转发服务器配置如下 监听80端口,在http里面配置如下 将edc.com分别转发到149和150的服务器上 本地主 ...

  6. (匹配 最小路径覆盖)Air Raid --hdu --1151

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1151 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  7. angular2+ 初理解

    一.Angular Module     1.angular 模块是一个类,它需要NgModule这个装饰器函数接受一个原数据对象作为参数来描述这个模块类属性.     其中最重要的属性有:      ...

  8. web.xml文件中context-param、listener、filter、servlet的执行顺序

    首先可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关.即不会因为 filter 写在 listener 的前面而会先加载 filter.最终得出的结论是:listener -> ...

  9. 获得硬盘的ID序列号(XE10.1+WIN8.1)

    疯狂delphi DelphiXE公开课群:100162924.58593121 朱建强QQ:513187410 获得硬盘的ID序列号(XE10.1+WIN8.1) 相关资料: https://zhi ...

  10. DevExpress中Tile Application窗体的模型架构图

    DEV中Tile Application模型架构比较复杂,整理一下和大家分享. 图中:立体代表类:虚线椭圆代表属性.