Array

struct Array<Element> : RandomAccessCollection, MutableCollection
  • Returns the nth element in the array or nil.

    let arr = [1, 2, 3]
    arr.at(0) #=> 1
    arr.at(4) #=> nil
    

    Declaration

    Swift

    func at(_ num: Int) -> Element?

    Parameters

    num

    The element index

    Return Value

    An element at specific index or nil

  • Choose a random element from the array.

    let arr = [1, 2, 3]
    arr.sample      #=> 2
    arr.sample      #=> 1
    arr.sample      #=> 1
    arr.sample      #=> 2
    arr.sample      #=> 3
    arr             #=> [1, 2, 3]
    

    If the array is empty Array#sample returns nil.

    [].sample       #=> nil
    

    Declaration

    Swift

    var sample: Element?
  • Choose a random element or n random elements from the array. The elements are chosen by using random and unique indices into the array in order to ensure that an element doesn’t repeat itself unless the array already contained duplicate elements. If the array is empty the Array#sample returns nil and the Array#sample(n:) form returns an empty array.

    let arr = [1, 2, 3]
    arr.sample(1)       #=> [2]
    arr.sample(2)       #=> [1, 2]
    arr.sample(2)       #=> [1, 3]
    arr.sample(2)       #=> [3, 1]
    arr.sample(3)       #=> [3, 1, 2]
    arr.sample(4)       #=> [2, 3, 1]
    arr.sample(0)       #=> []
    arr                 #=> [1, 2, 3]
    

    If the array is empty Array#sample(n:) returns an empty array.

    [].sample()         #=> []
    

    Declaration

    Swift

    func sample(_ n: Int = 1) -> [Element]

    Parameters

    n

    An integer of random elements count

    Return Value

    An new array with random elements

  • Returns a new array with elements of self shuffled.

    let arr = [1, 2, 3]
    arr.shuffle         #=> [1, 2, 3]
    arr.shuffle         #=> [3, 2, 1]
    arr.shuffle         #=> [2, 1, 3]
    arr.shuffle         #=> [1, 3, 2]
    arr                 #=> [1, 3, 2]
    

    Declaration

    Swift

    var shuffle: [Element]
  • Returns the tail of the array from the position.

    ["a", "b", "c", "d"].from(0)        #=> ["a", "b", "c", "d"]
    ["a", "b", "c", "d"].from(2)        #=> ["c", "d"]
    ["a", "b", "c", "d"].from(10)   #=> []
    [].from(0)                      #=> []
    ["a", "b", "c", "d"].from(-2)   #=> ["c", "d"]
    ["a", "b", "c"].from(-10)       #=> []
    

    Declaration

    Swift

    func from(_ position: Int) -> [Element]

    Parameters

    position

    An integer value.

    Return Value

    An sub array of Element.

  • Returns the beginning of the array up to the position.

    ["a", "b", "c", "d"].to(0)      #=> ["a"]
    ["a", "b", "c", "d"].to(2)      #=> ["a", "b", "c"]
    ["a", "b", "c", "d"].to(10)     #=> ["a", "b", "c", "d"]
    [].to(0)                        #=> []
    ["a", "b", "c", "d"].to(-2)     #=> ["a", "b", "c"]
    ["a", "b", "c"].to(-10)         #=> []
    

    Declaration

    Swift

    func to(_ position: Int) -> [Element]

    Parameters

    position

    An integer value.

    Return Value

    An sub array of Element.

  • Returns a copy of the Array without the specified elements.

    let people = ["David", "Rafael", "Aaron", "Todd"]
    people.without("David", "Aaron")        #=> ["Rafael", "Todd"]
    

    Declaration

    Swift

    func without(_ elements: Element...) -> [Element]

    Parameters

    elements

    An array of specific elements.

    Return Value

    An new array.

  • Makes the current string empty

    var arr = [1, 2, 3]
    arr.clear()     #=> []
    arr             #=> []
    

    Declaration

    Swift

    mutating func clear() -> [Element]

    Return Value

    An empty string

  • Remove last element in array or nil

    var arr = [1, 2, 3]
    arr.pop()       #=> 3
    arr.pop()       #=> 2
    arr.pop()       #=> 1
    arr.pop()       #=> nil
    arr             #=> []
    

    Declaration

    Swift

    mutating func pop() -> Element?

    Return Value

    The last element in array

  • Return the last several elements in array.

    var arr = [1, 2, 3]
    arr.pop()       #=> [3]
    arr.pop(2)      #=> [1, 2]
    arr             #=> []
    arr.pop()       #=> []
    arr.pop()       #=> []
    arr             #=> []
    

    Declaration

    Swift

    mutating func pop(_ num: Int) -> [Element]

    Parameters

    num

    The count of returning elements

    Return Value

    An new array of popped element

  • Appends objects to the front of self.

    var arr = ["a", "b", "c"]
    arr.push("d")           #=> ["a", "b", "c", "d"]
    arr.push("e", "f")      #=> ["a", "b", "c", "d", "e", "f"]
    arr                     #=> ["a", "b", "c", "d", "e", "f"]
    

    See

    See also: Array#pop(num:) for the opposite effect.

    Declaration

    Swift

    mutating func push(_ objs: Element...) -> [Element]

    Parameters

    objs

    An array of object prepend to the receiver array

    Return Value

    An array with objs append to self

  • Remove first element in array or nil

    var arr = ["a", "b", "c"]
    arr.shift()     #=> "a"
    arr             #=> ["b", "c"]
    arr.shift()     #=> "b"
    arr.shift()     #=> "c"
    arr.shift()     #=> nil
    arr             #=> []
    

    Declaration

    Swift

    mutating func shift() -> Element?

    Return Value

    The first element in array

  • Return the first several elements in array.

    var arr = ["a", "b", "c"]
    arr.shift()     #=> "a"
    arr             #=> ["b", "c"]
    arr.shift(2)     #=> ["b", "c"]
    arr.shift()     #=> []
    arr             #=> []
    

    Declaration

    Swift

    mutating func shift(_ num: Int) -> [Element]

    Parameters

    num

    The count of returning elements

    Return Value

    An new array of shifted element

  • Prepends objects to the front of self, moving other elements upwards

    var arr = [1]
    arr.unshift(2)      #=> [2, 1]
    arr.unshift(2, 3)   #=> [2, 3, 2, 1]
    arr                 #=> [2, 3, 2, 1]
    

    See

    See also: Array#shift(num:) for the opposite effect.

    Declaration

    Swift

    mutating func unshift(_ objs: Element...) -> [Element]

    Parameters

    objs

    An array of object prepend to the receiver array

    Return Value

    An array with objs prepend to self

  • A mutating version of Sequence#select(closure:)

    var arr1 = [1, 2, 3]
    arr1.select { $0 > 2 }       #=> [3]
    arr1                         #=> [3]
    
    var arr2 = [1, 2, 3]
    arr2.select { $0 <= 2 }      #=> [1, 2]
    arr2                         #=> [1, 2]
    
    var arr3 = [1, 2, 3]
    arr3.select { _ in false }   #=> []
    arr3                         #=> []
    

    Declaration

    Swift

    mutating func selected(closure: (Element) throws -> Bool) rethrows -> [Element]

    Parameters

    closure

    A block accepts element in the receiver and returns a bool value

    Return Value

    Self

  • A mutating version of Sequence#reject(closure:)

    var arr1 = [1, 2, 3]
    arr1.reject { $0 > 2 }       #=> [1, 2]
    arr1                         #=> [1, 2]
    
    var arr2 = [1, 2, 3]
    arr2.reject { $0 <= 2 }      #=> [3]
    arr2                         #=> [3]
    
    var arr3 = [1, 2, 3]
    arr3.reject { _ in false }   #=> [1, 2, 3]
    arr3                         #=> [1, 2, 3]
    

    Declaration

    Swift

    mutating func rejected(closure: (Element) throws -> Bool)rethrows  -> [Element]

    Parameters

    closure

    A block accepts element in the receiver and returns a bool value

    Return Value

    Self

  • Deletes all items from self that are equal to obj. If all is false, delete the first object the the array.

    var arr = [1, 2, 3, 4, 1, 2, 3, 5, 6]
    arr.delete(1)                   #=> 1
    arr                             #=> [2, 3, 4, 2, 3, 5, 6]
    arr.delete(2)                   #=> 2
    arr                             #=> [3, 4, 3, 5, 6]
    arr.delete(3, all: false)       #=> 3
    arr                             #=> [4, 3, 5, 6]
    arr.delete(1000)                #=> nil
    arr                             #=> [4, 3, 5, 6]
    

    Declaration

    Swift

    mutating func delete(_ obj: Element, all: Bool = true) -> Element?

    Parameters

    obj

    An object which will be deleted in the array

    all

    A bool value indicates whether deletes all the same object in array (default is true).

    Return Value

    The deleted object or nil.

  • Convert an array of integer to int by base default is 10.

    [1,2,3].to_i()      #=> 321
    [1,2,3,4].to_i()    #=> 4321
    [0,0,0,1].to_i(2)   #=> 8
    

    Declaration

    Swift

    func to_i(_ base: Int = 10) -> Int

    Parameters

    base

    An integer indicates the integer’s base

    Return Value

    An integer

  • Converts the array to a comma-separated sentence where the last element is joined by the connector word.

    [].to_sentence                          #=> ""
    ["one"].to_sentence                     #=> "one"
    ["one", "two"].to_sentence              #=> "one and two"
    ["one", "two", "three"].to_sentence     #=> "one, two, and three"
    ["one", "two"].to_sentence(twoWordsConnector: "-")      #=> "one-two"
    ["one", "two", "three"].to_sentence(wordsConnector: " or ", lastWordConnector: " or at least ") 
        #=> "one or two or at least three"
    

    Declaration

    Swift

    var to_sentence: String
  • Converts the array to a comma-separated sentence where the last element is joined by the connector word.

    [].to_sentence                          #=> ""
    ["one"].to_sentence                     #=> "one"
    ["one", "two"].to_sentence              #=> "one and two"
    ["one", "two", "three"].to_sentence     #=> "one, two, and three"
    ["one", "two"].to_sentence(twoWordsConnector: "-")      #=> "one-two"
    ["one", "two", "three"].to_sentence(wordsConnector: " or ", lastWordConnector: " or at least ")
         #=> "one or two or at least three"
    

    Declaration

    Swift

    func to_sentence(wordsConnector: String = ", ", twoWordsConnector: String = " and ", lastWordConnector: String = ", and ") -> String

    Parameters

    wordsConnector

    The sign or word used to join the elements in arrays with two or more elements (default: ,).

    twoWordsConnector

    The sign or word used to join the elements n arrays with two elements (default: and).

    lastWordConnector

    The sign or word used to join the last element in arrays with three or more elements (default: , and).

    Return Value

    A sentence joined the array of string into an human-readable sentence.

  • Splits or iterates over the array in groups of size number, padding any remaining slots with with unless it is nil.

    arr.inGroup(of: 0)              #=> []
    arr.inGroup(of: 3)              #=> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
    arr.inGroup(of: 4)              #=> [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10]]
    arr.inGroup(of: 4, fill: 0)     #=> [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 0, 0]]
    
    arr.inGroup(of: 4, fill: 0) { group in
        print(group)    #=> [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 0, 0]
    }
    

    Declaration

    Swift

    func inGroup(of num: Int, fill with: Element? = nil, closure: (([Element]) -> Void)? = nil) -> [[Element]]

    Parameters

    num

    The size of array in the two dimentional array.

    with

    An element used to padding remaining slot in the last array.

    closure

    A closure recevies array as parameter

    Return Value

    A two dimentional array of Element.

  • Splits or iterates over the array in number of groups, padding any remaining slots with with unless it is false.

    arr.inGroup(0)              #=> []
    arr.inGroup(3)              #=> [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
    arr.inGroup(4)              #=> [[1, 2, 3], [4, 5, 6], [7, 8], [9, 10]]
    arr.inGroup(4, fill: 0)     #=> [[1, 2, 3], [4, 5, 6], [7, 8, 0], [9, 10, 0]]
    
    arr.inGroup(4, fill: 0) { group in
        print(group)    #=> [1, 2, 3], [4, 5, 6], [7, 8, 0], [9, 10, 0]
    }
    

    Declaration

    Swift

    func inGroup(_ num: Int, fill with: Element? = nil, closure: (([Element]) -> Void)? = nil) -> [[Element]]

    Parameters

    num

    The size of array in the two dimentional array.

    with

    An element used to padding remaining slot in the last array.

    closure

    A closure recevies array as parameter

    Return Value

    A two dimentional array of Element.

  • Divides the array into one or more subarrays based on a delimiting value or the result of an block.

    let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    arr.split(3)    #=> [[1, 2], [4, 5, 6, 7, 8, 9, 10]]
    
    arr.split { 
        $0 % 3 == 0 
    }   #=> [[1, 2], [4, 5], [7, 8], [10]]
    

    Declaration

    Swift

    func split(_ closure: (Element) -> Bool) -> [[Element]]

    Parameters

    closure

    A closure accepts element and returns bool value.

    Return Value

    A nested array.

  • Divides the array into one or more subarrays based on a delimiting value or the result of an block.

    let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    arr.split(3)    #=> [[1, 2], [4, 5, 6, 7, 8, 9, 10]]
    
    arr.split {
        $0 % 3 == 0
    }   #=> [[1, 2], [4, 5], [7, 8], [10]]
    

    Declaration

    Swift

    func split(_ value: Element) -> [[Element]]

    Parameters

    value

    A value used to split the array.

    Return Value

    A nested array.

  • Returns true if self and other are the same object, or are both arrays with the same content

    let arr1 = [1, 2, 3]
    let arr2 = [3, 2, 1]
    arr1.isEql(arr2)    #=> true
    
    let arr3 = [3, 2, 3]
    let arr4 = [3, 2, 1]
    arr3.isEql(arr4)    #=> false
    
    let arr5 = [1, 2, 3, 4, 5, 6]
    let arr6 = [3, 2, 1]
    arr5.isEql(arr6)    #=> false
    

    Declaration

    Swift

    func isEql(_ other: [Element]) -> Bool

    Parameters

    other

    Another array

    Return Value

    A bool value indicates the equatable between two arrays

  • Returns true if the given object is present in self (that is, if any element == object).

    [1, 2, 3].isInclude(1)              #=> true
    ["a", "b", "c"].isInclude("b")      #=> true
    

    Otherwise returns false.

    [1, 2, 3].isInclude(100)            #=> false
    ["a", "b", "c"].isInclude("bbb")        #=> false
    

    Declaration

    Swift

    func isInclude(_ value: Element) -> Bool

    Parameters

    value

    An Element value.

    Return Value

    A bool value.

  • Returns a new array with unique element.

    let arr = [1, 2, 3, 4, 1, 2]
    arr.uniq        #=> [1, 2, 3, 4]
    arr             #=> [1, 2, 3, 4, 1, 2]
    

    Declaration

    Swift

    var uniq: [Element]
  • When invoked with a closure, pass all combinations of length n of elements from the array and then returns the array itself. The implementation makes no guarantees about the order in which the combinations are yielded.

    let arr = [1, 2, 3]
    arr.combination(1) #=> [[1], [2], [3]]
    arr.combination(2) #=> [[1, 2], [1, 3], [2, 3]]
    arr.combination(3) #=> [[1, 2, 3]]
    arr.combination(0) #=> []
    arr.combination(5) #=> [[]]
    

    Declaration

    Swift

    func combination(_ num: Int, closure: (([Element]) -> Void)? = nil) -> [[Element]]

    Parameters

    num

    The length of combination in the returning array

    closure

    A closure called each time finds a new combination

    Return Value

    An new array with all the possible combination in the receiver array

  • When invoked with a closure, pass all repeated combinations of length n of elements from the array and then returns the array itself. The implementation makes no guarantees about the order in which the combinations are yielded.

    let arr = [1, 2, 3]
    arr.repeatedCombination(1) #=> [[1],[2],[3]]
    arr.repeatedCombination(2) #=> [[1,1],[1,2],[1,3],[2,2],[2,3],[3,3]]
    arr.repeatedCombination(3) #=> [[1,1,1],[1,1,2],[1,1,3],[1,2,2],[1,2,3],[1,3,3],[2,2,2],[2,2,3],[2,3,3],[3,3,3]]
    arr.repeatedCombination(4) #=> [[1,1,1,1],[1,1,1,2],[1,1,1,3],[1,1,2,2],[1,1,2,3],[1,1,3,3],
        [1,2,2,2],[1,2,2,3],[1,2,3,3],[1,3,3,3], [2,2,2,2],[2,2,2,3],[2,2,3,3],[2,3,3,3],[3,3,3,3]]
    arr.repeatedCombination(0) #=> []
    

    Declaration

    Swift

    func repeatedCombination(_ num: Int, closure: (([Element]) -> Void)? = nil) -> [[Element]]

    Parameters

    num

    The length of combination in the returning array

    closure

    A closure called each time finds a new combination

    Return Value

    An new array with all the possible repeated combination in the receiver array

  • Extracts the nested value specified by the sequence of idx objects by calling dig at each step, returning nil if any intermediate step is nil.

    let a = [[1, 2, 3], [3, 4, 5]]
    a.dig(0)                    #=> [1, 2, 3]
    a.dig(1, 2)                 #=> 5
    a.dig(1, 2, 3)              #=> nil
    a.dig(10, 2, 3)             #=> nil
    

    Declaration

    Swift

    func dig<T>(_ idxs: Int...) -> T?

    Parameters

    idxs

    A sequence of int specify the value location in the recevier array.

    Return Value

    An value in the nested array or nil

  • Extracts the nested value specified by the sequence of idx objects by calling dig at each step, returning nil if any intermediate step is nil.

    let a = [[1, 2, 3], [3, 4, 5]]
    a.dig(0)                    #=> [1, 2, 3]
    a.dig(1, 2)                 #=> 5
    a.dig(1, 2, 3)              #=> nil
    a.dig(10, 2, 3)             #=> nil
    

    Declaration

    Swift

    func dig<T>(_ idxs: [Int]) -> T?

    Parameters

    idxs

    A sequence of int specify the value location in the recevier array.

    Return Value

    An value in the nested array or nil

  • Converts any arguments to arrays, then merges elements of self with corresponding elements from each argument.

    let a = [ 4, 5, 6 ]
    let b = [ 7, 8, 9 ]
    [1, 2, 3].zip(a, b)   #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
    

    This generates a nested n-element arrays, where n is the least count of all the ararys including the receiver.

    [1, 2].zip(a, b)      #=> [[1, 4, 7]]
    a.zip([1, 2], [8])    #=> [[4, 1, 8]]
    

    Declaration

    Swift

    func zip<T>(_ arrays: [T]...) -> [[T]]

    Parameters

    arrays

    Another arrays with type [T]

    Return Value

    An new nested array

  • Returns the tranpose of the current two-dimentional array.

    let a = [[1, 2], [3, 4], [5, 6]]
    a.transpose()   #=> [[1, 3, 5], [2, 4, 6]]
    

    If the receiver’s elements have different length, Array#transpose() will return nil.

    let b = [[1, 2], [3], [5, 6, 7]]
    b.transpose()   #=> nil
    

    Declaration

    Swift

    func tranpose<T>() -> [[T]]?

    Return Value

    An new array or nil

  • Returns a new array by rotating self so that the element at count is the first element of the new array.

    let a = [ "a", "b", "c", "d" ]
    a.rotate         #=> ["b", "c", "d", "a"]
    a                #=> ["a", "b", "c", "d"]
    

    See

    See also: Array#rotate(count:)

    Declaration

    Swift

    var rotate: [Element]
  • Returns a new array by rotating self so that the element at count is the first element of the new array.

    let a = [ "a", "b", "c", "d" ]
    a.rotate         #=> ["b", "c", "d", "a"]
    a                #=> ["a", "b", "c", "d"]
    a.rotate(2)      #=> ["c", "d", "a", "b"]
    

    If count is negative then it rotates in the opposite direction, starting from the end of self where -1 is the last element.

    a.rotate(-3)     #=> ["b", "c", "d", "a"]
    

    Declaration

    Swift

    func rotate(_ count: Int = 1) -> [Element]

    Parameters

    count

    The element in the first place after rotate

    Return Value

    A new array

  • Returns a new string by concatenating the elements of the sequence, adding the given separator between each element.

    The following example shows how an array of strings can be joined to a single, comma-separated string:

    let cast = ["Vivien", "Marlon", "Kim", "Karl"]
    let list = cast.join(", ")
    print(list)
    // Prints "Vivien, Marlon, Kim, Karl"
    

    Declaration

    Swift

    func join(_ separator: String = "") -> String

    Parameters

    separator

    A string to insert between each of the elements in this sequence. The default separator is an empty string.

    Return Value

    A single, concatenated string.