协议
协议-Sequence
public protocol Sequence {
/// 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
/// A type that represents a subsequence of some of the sequence's elements.
// associatedtype SubSequence: Sequence = AnySequence<Element>
// where Element == SubSequence.Element,
// SubSequence.SubSequence == SubSequence
// typealias SubSequence = AnySequence<Element>
/// Returns an iterator over the elements of this sequence.
__consuming func makeIterator() -> Iterator
// 省略...
}
协议-Collection
通过下面源码可以知道,Collection
继承了Sequence
协议
public protocol Collection: Sequence {
// FIXME: ideally this would be in MigrationSupport.swift, but it needs
// to be on the protocol instead of as an extension
@available(*, deprecated/*, obsoleted: 5.0*/, message: "all index distances are now of type Int")
typealias IndexDistance = Int
// FIXME: Associated type inference requires this.
override associatedtype Element
// 省略...
}
协议-MutableCollection
public protocol MutableCollection: Collection
where SubSequence: MutableCollection
{
// FIXME: Associated type inference requires these.
override associatedtype Element
override associatedtype Index
override associatedtype SubSequence
/// Accesses the element at the specified position.
///
/// For example, you can replace an element of an array by using its
/// subscript.
///
/// var streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"]
/// streets[1] = "Butler"
/// print(streets[1])
/// // Prints "Butler"
///
/// You can subscript a collection with any valid index other than the
/// collection's end index. The end index refers to the position one
/// past the last element of a collection, so it doesn't correspond with an
/// element.
///
/// - Parameter position: The position of the element to access. `position`
/// must be a valid index of the collection that is not equal to the
/// `endIndex` property.
///
/// - Complexity: O(1)
@_borrowed
override subscript(position: Index) -> Element { get set }
Dictionary遵守Sequence
Dictionary
结构体遵守了Sequence
协议
extension Dictionary: Sequence {
/// Returns an iterator over the dictionary's key-value pairs.
///
/// Iterating over a dictionary yields the key-value pairs as two-element
/// tuples. You can decompose the tuple in a `for`-`in` loop, which calls
/// `makeIterator()` behind the scenes, or when calling the iterator's
/// `next()` method directly.
///
/// let hues = ["Heliotrope": 296, "Coral": 16, "Aquamarine": 156]
/// for (name, hueValue) in hues {
/// print("The hue of \(name) is \(hueValue).")
/// }
/// // Prints "The hue of Heliotrope is 296."
/// // Prints "The hue of Coral is 16."
/// // Prints "The hue of Aquamarine is 156."
///
/// - Returns: An iterator over the dictionary with elements of type
/// `(key: Key, value: Value)`.
@inlinable
@inline(__always)
public __consuming func makeIterator() -> Iterator {
return _variant.makeIterator()
}
}
Array遵守MutableCollection
Array
结构体遵守的协议RandomAccessCollection
、MutableCollection
最终都是Collection
。
extension Array: RandomAccessCollection, MutableCollection {
/// The index type for arrays, `Int`.
public typealias Index = Int
/// The type that represents the indices that are valid for subscripting an
/// array, in ascending order.
public typealias Indices = Range<Int>
/// The type that allows iteration over an array's elements.
public typealias Iterator = IndexingIterator<Array>
/// The position of the first element in a nonempty array.
///
/// For an instance of `Array`, `startIndex` is always zero. If the array
/// is empty, `startIndex` is equal to `endIndex`.
@inlinable
public var startIndex: Int {
return 0
}