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

  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>)

原文地址:https://www.cnblogs.com/huahuahu/p/Swift-li-ji-he-lei-xing-xie-yi-de-guan-xi.html

时间: 2024-07-31 22:50:28

Swift 里集合类型协议的关系的相关文章

Swift中的集合类型

一.引子: 在2014年10月TIOBE编程语言排行榜中,Swift位居第18位,从2014WWDC发布会首次公布至今不到半年时间,swift一直受到编程人员的追捧,其热衷程度并不亚于当红巨星Taylor Swift.相信在不远的将来,swift能够平稳发展,并逐步取代Objective-C. 二.swift的集合类型 下面回归主题.作为一名iOS开发者,我们已经非常熟悉诸如NSArray,NSDictionary,NSSet等常见集合类型,以及它们的可变同类NSMutableArray,NSM

swift 集合类型(二)

说到swift的集合类型,就不得不谈到Dictionary.包含一个键值对组合的集合. var air = ["name":"warner","title":"Math"] var air = Dictionary<String,String>(minimumCapacity:3) 都可以初始化Dictionary.在swift中,Dictionary其实是一个结构:struct,继承自Collection.而Co

swift 初见-5集合类型

// Playground - noun: a place where people can play import UIKit var str = "Hello, playground" // swift的集合类型只有数组和字典 // 数组中只能存储相同类型的数据(可以是基本类型,也可以是类对象) // 申明方法 [String] Array<String> // 一旦隐是声明后,他的类型就确定了,不能在存储其他类型的数据了 var array=["a"

Swift学习笔记 - 教程学习三 集合类型 (Collection Types)

集合类型 (Collection Types) 本节及前面用到的for…in句型在后面的循环控制部分,if let 见基础篇.如果某些字符看不到,请到博客园来看原文.——新波 Swift提供了三种基本集合类型,数组(array).集合(set)和字典(dictionary).数组是一组按序排列的数据,集合是一组各不相同的无序数据,字典是一组与关键值相关联的无序数据.参见下图. 3.1 集合的可变性Mutability of Collections 与前面的字符串一样,赋值给变量的集合是可变的,赋

Welcome to Swift (苹果官方Swift文档初译与注解二十一)---140~147页(第三章--集合类型)

第三章 Collection Types (集合类型) 在Swift中,提供了两种集合类型用来存储一组值:数组和字典.数组有序的存储相同类型的值;字典存储无序的相同类型的值.字典可以通过唯一的标识(就是所说的键)来查询和访问. 在Swift中,数组和字典总是要清晰的标明他们存储数据的类型.这就意味着不可以将错误的类型插入到数组或字典中.同时也意味着你是明确了解你要遍历的数组或字典里面数据的类 型.在Swift中,集合要显式的声明类型来保证在开发中都会明确的知道它能处理的数据类型. 注意点: 在S

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

这篇文章主要讲解苹果Swift官方指南的第二章前四节的要点内容,如果想看完整的英文文档可以去苹果开发者页面下载. Basic 声明常量let 声明变量var 注释依旧使用"//" "/**/", 注意这里"/**/"在Swift可以嵌套使用 表达式结尾不再有分号 整数 你可以声明为Int或者UInt,他们本身适应不同平台(64位, 32位),类似于NSInteger,NSUInteger 也可以直接声明为指定字节数的Int,如: Int8, In

Swift学习笔记(5):集合类型

目录: 数组:Array 集合:Set 字典:Dictionary Swift提供Array(有序集合数据).Set(无序无重复集合)和Dictionary(无序键值对集合)三种基本集合类型来存储明确数据类型的集合数据. 使用var将集合声明为变量,可以在创建之后添加.移除.修改集合内数据项.如果使用let将集合声明为常量,则它的大小和内容就都不可改变. 数组:Array 初始化和赋值: var someInts = [Int]() // 创建指定数据类型的空数组 someInts = [] /

Welcome to Swift (苹果官方Swift文档初译与注解二十三)---154~162页(第三章--集合类型)

Dictionaries (字典) 字典像是一个容器,它可以存放很多相同类型的值.每个值都有与之关联的唯一的键,键在字典里的作用,就像是每个值的身份证标识一样.与数组中的元素不同,字典里的每个元素没有 固定的循序.当你使用字典并且要查询一个值的时候,需要使用值的标识(key)才行.这就像是你在生活里使用一本字典来查找某个单词的定义一样. 在Swift中,字典能够存储的类型需要明确定义.这与OC中的NSDictionary 类和NSMutableDictionary 类很不同,OC中它们可以使用任

[Swift]Day03:集合类型

集合类型 数组 重复值的初始化 除了普通的初始化方法,我们可以通过 init(count: Int, repeatedValue: T) 来初始化一个数组并填充上重复的值: // [0.0,0.0,0.0] var threeDoubles = [Double](count:3,repeatedValue:0.0) 带索引值的遍历 我们可以用 for in 遍历数组,如果想要 index 的话,可以用 enumerate<Seq : SequenceType>(base: Seq) : let