Collection

protocol Collection : Sequence where Self.SubSequence : Collection
  • 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.