Summary
Collection extends Sequence. It guarantees that the sequence is multi-pass, and it allows you to look up elements by their indices. It also adds slicing capabilities via its SubSequence type, which is a collection itself.
Types
IndexingIterator
IndexingIterator是collection类型默认的iterator实现- 自定义Collection所需要的最小实现只需要startIndex, endIndex, subscript方法
IndexingIterator实现了IteratorProtocol,Sequence,Sendable等protocol
1 | /// A type that iterates over a collection using its indices. |
Collection
A sequence whose elements can be traversed multiple times, nondestructively, and accessed by an indexed subscript.
- Accessing Individual Elements
- Accessing Slices of a Collection
- Traversing a Collection
- Conforming to the Collection Protocol
- The
startIndexandendIndexproperties - A subscript
index(after:)
- The
- Expected Performance
startIndexandendIndexand subscript access O(1)- forward or bidirectional collection accessing its
countproperty is an O(n)
1 | public protocol Collection: Sequence { |
Functions
randomeElement
1 | /// 这两个方法我觉得可以用在测试代码中,如可以方便的随机选取登陆用户账号密码用于测试,或者随机一个dataset传入要测试的方法中 |
popFirst
1 | /// Removes and returns the first element of the collection. |
isEmpty
1 | /// isEmpty 有什么值得写的?请认真看看下面的注视 |