Array

public extension Array
  • Groups elements by the criteria provided.

    Declaration

    Swift

    func group<K>(by keyForElement: (Element) -> K) -> [K : [Element]] where K : Hashable

    Parameters

    keyForElement

    returns the appropiate key for each element.

    Return Value

    Dictionary with elements grouped by their corresponding keys.

  • Returns a new array with element appended to the Array.

    Complexity

    Amortized O(1) unless self’s storage is shared with another live array; O(count) if self does not wrap a bridged NSArray; otherwise the efficiency is unspecified.

    Seealso

    append().

    Declaration

    Swift

    func appending(_ element: Element) -> [Element]
  • Returns the indexth element.

    Complexity

    Reading is O(1). Writing is O(1) unless self’s storage is shared with another live array; O(count) if self does not wrap a bridged NSArray; otherwise the efficiency is unspecified..

    Note

    This function should be used over subscript when we don’t want a runtime error when indexth element doesn’t exist.

    Warning

    Returns .none if the index is out of bound.

    Seealso

    subcript()

    Declaration

    Swift

    func get(_ index: Int) -> Element?

    Return Value

    The Element or nil if the array doesn’t contain an element in that index