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

    public mutating func prepend(_ newElement: Element)

    Parameters

    newElement

    element to insert.

  • SwifterSwift: Safely Swap values at 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

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

    Parameters

    index

    index of first element.

    otherIndex

    index of other element.

  • SwifterSwift: Keep elements of Array while condition is true.

    [0, 2, 4, 7].keep( where: {$0 % 2 == 0}) -> [0, 2, 4]
    

    Throws

    provided condition exception.

    Declaration

    Swift

    @discardableResult
    public mutating func keep(while condition: (Element) throws -> Bool) rethrows -> [Element]

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    self after applying provided condition.

  • SwifterSwift: Take element of Array while condition is true.

    [0, 2, 4, 7, 6, 8].take( where: {$0 % 2 == 0}) -> [0, 2, 4]
    

    Declaration

    Swift

    public func take(while condition: (Element) throws -> Bool) rethrows -> [Element]

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    All elements up until condition evaluates to false.

  • SwifterSwift: Skip elements of Array while condition is true.

    [0, 2, 4, 7, 6, 8].skip( where: {$0 % 2 == 0}) -> [6, 8]
    

    Declaration

    Swift

    public func skip(while condition: (Element) throws -> Bool) rethrows -> [Element]

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    All elements after the condition evaluates to false.

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

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

    Declaration

    Swift

    public 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 new rotated array by the given places.

    [1, 2, 3, 4].rotated(by: 1) -> [4,1,2,3]
    [1, 2, 3, 4].rotated(by: 3) -> [2,3,4,1]
    [1, 2, 3, 4].rotated(by: -1) -> [2,3,4,1]
    

    - Parameter places: Number of places that the array be rotated. If the value is positive the end becomes the start, if it negative it’s that start becom the end. - Returns: The new rotated array

    Declaration

    Swift

    public func rotated(by places: Int) -> [Element]
  • SwifterSwift: Rotate the array by the given places.

    [1, 2, 3, 4].rotate(by: 1) -> [4,1,2,3]
    [1, 2, 3, 4].rotate(by: 3) -> [2,3,4,1]
    [1, 2, 3, 4].rotated(by: -1) -> [2,3,4,1]
    

    Declaration

    Swift

    @discardableResult
    public mutating func rotate(by places: Int) -> [Element]

    Parameters

    places

    The number of places that the array should be rotated. If the value is positive the end becomes the start, if it negative it’s that start become the end.

    Return Value

    self after rotating.

  • SwifterSwift: Shuffle array. (Using Fisher-Yates Algorithm)

    [1, 2, 3, 4, 5].shuffle() // shuffles array
    

    Declaration

    Swift

    @discardableResult
    public mutating func shuffle() -> [Element]

    Return Value

    self after shuffling.

  • SwifterSwift: Shuffled version of array. (Using Fisher-Yates Algorithm)

    [1, 2, 3, 4, 5].shuffled // return a shuffled version from given array e.g. [2, 4, 1, 3, 5].
    

    Declaration

    Swift

    public func shuffled() -> [Element]

    Return Value

    the array with its elements shuffled.

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

    Declaration

    Swift

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

    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

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

    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
    public mutating func sort<T: Comparable>(by path: KeyPath<Element, T?>, ascending: Bool = true) -> [Element]

    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
    public mutating func sort<T: Comparable>(by path: KeyPath<Element, T>, ascending: Bool = true) -> [Element]

    Parameters

    path

    Key path to sort, must be Comparable.

    ascending

    whether order is ascending or not.

    Return Value

    self after sorting.

  • SwifterSwift: Removes the first element of the collection which satisfies the given predicate.

       [1, 2, 2, 3, 4, 2, 5].removeFirst { $0 % 2 == 0 } -> [1, 2, 3, 4, 2, 5]
       ["h", "e", "l", "l", "o"].removeFirst { $0 == "e" } -> ["h", "l", "l", "o"]
    

    Declaration

    Swift

    @discardableResult
    public mutating func removeFirst(where predicate: (Element) throws -> Bool) rethrows -> Element?

    Parameters

    predicate

    A closure that takes an element as its argument and returns a Boolean value that indicates whether the passed element represents a match.

    Return Value

    The first element for which predicate returns true, after removing it. If no elements in the collection satisfy the given predicate, returns nil.

  • SwifterSwift: All indices of specified item.

    [1, 2, 2, 3, 4, 2, 5].indices(of 2) -> [1, 2, 5]
    [1.2, 2.3, 4.5, 3.4, 4.5].indices(of 2.3) -> [1]
    ["h", "e", "l", "l", "o"].indices(of "l") -> [2, 3]
    

    Declaration

    Swift

    public func indices(of item: Element) -> [Index]

    Parameters

    item

    item to check.

    Return Value

    an array with all indices of the given item.

  • 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
    public 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
    public 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

    public mutating func removeDuplicates()
  • SwifterSwift: Return array with all duplicate elements removed.

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

    Declaration

    Swift

    public func withoutDuplicates() -> [Element]

    Return Value

    an array of unique elements.

  • SwifterSwift: Element at the given index if it exists.

    [1, 2, 3, 4, 5].item(at: 2) -> 3
    [1.2, 2.3, 4.5, 3.4, 4.5].item(at: 3) -> 3.4
    ["h", "e", "l", "l", "o"].item(at: 10) -> nil
    

    Declaration

    Swift

    @available(*, deprecated: 4.3, renamed: "subscript(safe:﹚", message: "Use subscript(safe:﹚ instead")
    public func item(at index: Int) -> Element?

    Parameters

    index

    index of element.

    Return Value

    optional element (if exists).

  • SwifterSwift: Group the elements of the array in a dictionary.

    [0, 2, 5, 4, 7].groupByKey { $0%2 ? "evens" : "odds" } -> [ "evens" : [0, 2, 4], "odds" : [5, 7] ]
    

    - Parameter getKey: Clousure to define the key for each element. - Returns: A dictionary with values grouped with keys.

    Declaration

    Swift

    @available(*, deprecated, message: "Use 'Dictionary.init(grouping:by:﹚' instead.")
    public func groupByKey<K>(keyForValue: (_ element: Element) throws -> K) rethrows -> [K : [Element]] where K : Hashable
  • SwifterSwift: Return array with all duplicate elements removed.

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

    Declaration

    Swift

    @available(*, deprecated: 4.3, renamed: "withoutDuplicates", message: "Use withoutDuplicates(﹚ instead")
    public func duplicatesRemoved() -> [Element]

    Return Value

    an array of unique elements.

  • SwifterSwift: All indexes of specified item.

    [1, 2, 2, 3, 4, 2, 5].indexes(of 2) -> [1, 2, 5]
    [1.2, 2.3, 4.5, 3.4, 4.5].indexes(of 2.3) -> [1]
    ["h", "e", "l", "l", "o"].indexes(of "l") -> [2, 3]
    

    Declaration

    Swift

    @available(*, deprecated: 4.1.1, renamed: "indices(of:﹚", message: "Use indices(of:﹚ instead")
    public func indexes(of item: Element) -> [Int]

    Parameters

    item

    item to check.

    Return Value

    an array with all indexes of the given item.

  • SwifterSwift: Remove last element from array and return it.

    [1, 2, 3, 4, 5].pop() // returns 5 and remove it from the array.
    [].pop() // returns nil since the array is empty.
    

    Declaration

    Swift

    @available(*, deprecated: 4.3, message: "Use popLast(﹚ instead")
    @discardableResult
    public mutating func pop() -> Element?

    Return Value

    last element in array (if applicable).

  • SwifterSwift: Insert an element to the end of array.

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

    Declaration

    Swift

    @available(*, deprecated: 4.3, message: "Use append(﹚ instead")
    public mutating func push(_ newElement: Element)

    Parameters

    newElement

    element to insert.

  • SwifterSwift: Swap values at index positions.

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

    Declaration

    Swift

    @available(*, deprecated: 4.3, message: "Use swapAt(﹚ instead")
    public mutating func swap(from index: Int, to otherIndex: Int)

    Parameters

    index

    index of first element.

    otherIndex

    index of other element.