Array

struct Array<Element> : _DestructorSafeContainer
  • SwifterSwift: Insert an element at the beginning of array.

       [2, 3, 4, 5].prepend(1) -> [1, 2, 3, 4, 5]
       ["e", "l", "l", "o"].prepend("h") -> ["h", "e", "l", "l", "o"]
    

    Declaration

    Swift

    mutating func prepend(_ newElement: Element)

    Parameters

    newElement

    element to insert.

  • SwifterSwift: Safely swap values at given index positions.

       [1, 2, 3, 4, 5].safeSwap(from: 3, to: 0) -> [4, 2, 3, 1, 5]
       ["h", "e", "l", "l", "o"].safeSwap(from: 1, to: 0) -> ["e", "h", "l", "l", "o"]
    

    Declaration

    Swift

    mutating func safeSwap(from index: Index, to otherIndex: Index)

    Parameters

    index

    index of first element.

    otherIndex

    index of other element.

  • SwifterSwift: Separates array into 2 arrays based on a given predicate.

    [0, 1, 2, 3, 4, 5].divided { $0 % 2 == 0 } -> ( [0, 2, 4], [1, 3, 5] )
    

    Declaration

    Swift

    func divided(by condition: (Element) throws -> Bool) rethrows -> (matching: [Element], nonMatching: [Element])

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    Two arrays, the first containing the elements for which the specified condition evaluates to true, the second containing the rest.

  • SwifterSwift: Returns a sorted array based on an optional keypath.

    Declaration

    Swift

    func sorted<T>(by path: KeyPath<Element, T?>, ascending: Bool = true) -> [Element] where T : Comparable

    Parameters

    path

    Key path to sort. The key path type must be Comparable.

    ascending

    If order must be ascending.

    Return Value

    Sorted array based on keyPath.

  • SwifterSwift: Returns a sorted array based on a keypath.

    Declaration

    Swift

    func sorted<T>(by path: KeyPath<Element, T>, ascending: Bool = true) -> [Element] where T : Comparable

    Parameters

    path

    Key path to sort. The key path type must be Comparable.

    ascending

    If order must be ascending.

    Return Value

    Sorted array based on keyPath.

  • SwifterSwift: Sort the array based on an optional keypath.

    Declaration

    Swift

    @discardableResult
    mutating func sort<T>(by path: KeyPath<Element, T?>, ascending: Bool = true) -> [Element] where T : Comparable

    Parameters

    path

    Key path to sort, must be Comparable.

    ascending

    whether order is ascending or not.

    Return Value

    self after sorting.

  • SwifterSwift: Sort the array based on a keypath.

    Declaration

    Swift

    @discardableResult
    mutating func sort<T>(by path: KeyPath<Element, T>, ascending: Bool = true) -> [Element] where T : Comparable

    Parameters

    path

    Key path to sort, must be Comparable.

    ascending

    whether order is ascending or not.

    Return Value

    self after sorting.

  • SwifterSwift: Remove all instances of an item from array.

       [1, 2, 2, 3, 4, 5].removeAll(2) -> [1, 3, 4, 5]
       ["h", "e", "l", "l", "o"].removeAll("l") -> ["h", "e", "o"]
    

    Declaration

    Swift

    @discardableResult
    mutating func removeAll(_ item: Element) -> [Element]

    Parameters

    item

    item to remove.

    Return Value

    self after removing all instances of item.

  • SwifterSwift: Remove all instances contained in items parameter from array.

       [1, 2, 2, 3, 4, 5].removeAll([2,5]) -> [1, 3, 4]
       ["h", "e", "l", "l", "o"].removeAll(["l", "h"]) -> ["e", "o"]
    

    Declaration

    Swift

    @discardableResult
    mutating func removeAll(_ items: [Element]) -> [Element]

    Parameters

    items

    items to remove.

    Return Value

    self after removing all instances of all items in given array.

  • SwifterSwift: Remove all duplicate elements from Array.

       [1, 2, 2, 3, 4, 5].removeDuplicates() -> [1, 2, 3, 4, 5]
       ["h", "e", "l", "l", "o"]. removeDuplicates() -> ["h", "e", "l", "o"]
    

    Declaration

    Swift

    @discardableResult
    mutating func removeDuplicates() -> [Element]

    Return Value

    Return array with all duplicate elements removed.

  • SwifterSwift: Return array with all duplicate elements removed.

    [1, 1, 2, 2, 3, 3, 3, 4, 5].withoutDuplicates() -> [1, 2, 3, 4, 5])
    ["h", "e", "l", "l", "o"].withoutDuplicates() -> ["h", "e", "l", "o"])
    

    Declaration

    Swift

    func withoutDuplicates() -> [Element]

    Return Value

    an array of unique elements.