Skip to main content

Array

An ordered, random-access collection that can grow dynamically, with elements of a single type.

var xs: [Int] = [1, 2, 3]

Accessing values​

var xs = [1, 2, 3]

// Get first element
var first = xs[0]

// Write to second element
xs[1] = 42

// Iterate over elements
for element in xs {
print("\{element}")
}

// Iterate over elements with both index and value retrieval
for i, element in xs.enumerated() {
print("\{i}, \{element}")
}

Printing​

Arrays can be interpolated into strings and print their contents.

var xs = [1, 2, 3]
print("\{xs}") // Prints [1, 2, 3]

Copying an Array​

The array type has reference semantics. Passing an array to a function allows it to mutate it. To copy an array use the copy method.

fun change(_ data: [Int]) {
data.append(5)
}

var original = [1, 2]
change(original) // changes original
print("\{original}") // prints [1, 2, 5]

var not_a_copy = original // doesn't make a copy
not_a_copy.append(10)
print("\{original}") // prints [1, 2, 5, 10]

var a_copy = original.copy()
a_copy.append(42)
print("\{original}") // prints [1, 2, 5, 10]

Properties​

first​

first: Element? { get }

The first element in the array if there is any.


is_empty​

is_empty: Bool { get }

Whether the array contains no elements.


last​

last: Element? { get }

The last element in the array if there is any.


length​

length: Int { get }

Number of elements in the array.

Methods​

all_satisfy​

all_satisfy(_ predicate: (Element) -> (Bool)) -> (Bool)

Returns whether all elements satisfy the predicate.

Returns true on an empty array.


append​

Appends elements to the end of the array. Available as several overloads:

Single Element​

append(_ element: Element)

Appends a single element to the end of the array.

Multiple Elements (8.12)​

append(_ other: [Element])

Appends all elements from another array other to the end of the array.


append_all (until 8.11)​

append_all(_ other: [Element])

Appends all elements from another array other to the end of the array.


clear​

clear()

Removes all elements.


contains​

contains(_ el: Element) -> (Bool)

Returns whether the given element is found in the array.

Requirements​

This method is only available if Element supports ==.


copy​

copy() -> ([Element])

Returns a copy of the array.


enumerated​

enumerated() -> (Enumerated<Element>)

Returns an enumerated sequence over the array with an iterator which provides both the index and the value.


filtered​

filtered(by should_be_included: (Element) -> (Bool)) -> ([Element])

Returns a new array filtered to the elements that should_be_included returned true for.


first_match​

first_match(where: (Element) -> (Bool)) -> (Element?)

Returns the first element that matches if there is any.


index​

index(of element: Element) -> (Int?)

Returns the first index of the element if it exists.

Requirements​

This method is only available if Element supports ==.


insert​

insert(_ element: Element, at: Int)

Inserts element at the specified index.


joined​

joined(separator: String) -> (String)

Combines all elements into a single string representation separating each element with separator.

Requirements​

This method is only available if the elements can be converted to string.


last_index​

last_index(of element: Element) -> (Int?)

Returns the last index of the element if it exists.

Requirements​

This method is only available if Element supports ==.


last_match​

last_match(where: (Element) -> (Bool)) -> (Element?)

Returns the last element that matches if there is any.


pop_last​

pop_last() -> (Element?)

Removes the last element and returns it.


remove​

remove(at index: Int) -> Element

Removes the element at index and returns it.


remove_all​

remove_all(where predicate: (Element) -> (Bool))

Removes all elements that match.


reverse​

reverse()

Reverse the elements in place.


reversed​

reversed() -> ([Element])

Returns a new array with the elements in reverse order.


some_satisfy​

some_satisfy(_ predicate: (Element) -> (Bool)) -> (Bool)

Returns whether there's at least one element matching predicate.

Returns false on an empty array.


sort​

sort()

Sorts the elements in ascending order in place

Requirements​

This method is only available if Element supports <.


sorted​

sorted() -> ([Element])

Returns a new array with the elements sorted in ascending order.

Requirements​

This method is only available if Element supports <.


sort_by​

sort_by(_ predicate: (Element, Element) -> (Bool))

Sorts the elements in place using predicate.

The predicate should return whether the first element should be sorted before the second element.


sorted_by​

sorted_by(_ predicate: (Element, Element) -> (Bool))

Returns a new array with the elements sorted using predicate.

The predicate should return whether the first element should be sorted before the second element.


subsequence​

subsequence(from: Int, length: Int? = nil) -> [Element]

Returns a new array containing the elements in the specified range.

from: Int

The starting index

length: Int? = nil

The number of elements to include after from. If not provided it will include the remainder after from.