Collection
protocol Collection : Sequence
-
Mechanica
Returns the element at the specified index or nil if not exists.
Example:
let array = [1, 2, 3] array[100] -> crash array[safe: 100] -> nil
Declaration
Swift
public subscript(safe index: Index) -> Element? { get }
Parameters
index
the index of the desired element.
-
Mechanica
Example:
let array = [1, 2, 3] array[100] -> crash array.at(100) -> nil
Declaration
Swift
public func at(_ index: Index) -> Element?
Parameters
index
the index of the desired element.
Return Value
The element at a given index or nil if not exists.
-
Mechanica
Returns all the indices of a specified item.
Example:
[1, 2, 1, 2, 3, 4, 5, 1].indices(of: 1) -> [0, 2, 7])
Declaration
Swift
public func indices(of item: Element) -> [Index]
Parameters
item
item to verify.
Return Value
all the indices of the given item.
-
Mechanica
Scans
self
exactly for a given count of elements, removing all the scanned elements fromself
.Example:
var phrase = "tin robots"[...] phrase.scan(count: 4) -> "tin " phrase -> robots
Declaration
Swift
public mutating func scan(count: Int) -> Self?
Parameters
count
The number of elements to be scanned.
Return Value
The scanned elements.
-
Mechanica
Scans
self
until a given condition is met, removing all the scanned elements fromself
and accumulating them into a given buffer.Example:
var phrase = "tin robots"[...] var buffer = String() phrase.scan { $0 == Character("o") } phrase -> "obots"
Declaration
Swift
public mutating func scan(upToCondition condition: ((Element) -> Bool))
Parameters
condition
The condition to be met.
-
Mechanica
Scans
self
until a given condition is met, removing all the scanned elements fromself
and accumulating them into a given buffer.Example:
var phrase = "tin robots"[...] var buffer = String() phrase.scan(upToCondition: { $0 == Character("o")}, into: &buffer) phrase -> "obots" buffer -> "tin r"
Declaration
Swift
public mutating func scan<C>(upToCondition condition: ((Element) -> Bool), into buffer: inout C) where C : RangeReplaceableCollection, Self.Element == C.Element
Parameters
condition
The condition to be met.
buffer
The buffer where all the scanned elements are accumulated.
-
Mechanica
Scans
self
for a specific index, removing all the scanned elements fromself
.Example:
var phrase = "tin robots"[...] phrase.scan(prefix: "tin") -> true phrase -> " robots") var phrase2 = "tin robots"[...] phrase2.scan(prefix: "in") -> false phrase2 -> "tin robots"
Declaration
Swift
public mutating func scan<C>(prefix: C) -> Bool where C : Collection, Self.Element == C.Element
Parameters
prefix
The prefix to be scanned.
Return Value
True is the prefix is met.
-
Mechanica
Scans
self
until a given collection is encountered, removing all the scanned elements fromself
and accumulating them into a given buffer.Example:
var phrase = "tin robots"[...] var buffer = String() phrase.scan(upToCollection: "obot", into: &buffer) phrase -> "obots" buffer -> "tin r"
Declaration
Swift
public mutating func scan<C>(upToCollection collection: Self, into buffer: inout C) where C : RangeReplaceableCollection, Self.Element == C.Element
Parameters
collection
The collection to be encountered.
buffer
The buffer where all the scanned elements are accumulated.
-
Mechanica
Scans
self
until a given collection is encountered, removing all the scanned elements fromself
.Example:
var phrase = "tin robots"[...] var buffer = String() phrase.scan(upToCollection: "obot") phrase -> "obots"
Declaration
Swift
public mutating func scan(upToCollection collection: Self)
Parameters
collection
The collection to be encountered.