Arrays
Arrays are ordered lists of values, all of the same data type and contained in the same structure. Swift, a very type-safe language, will throw compiler errors if values of different types are stored in a single array (e.g., a String
in an Int
array). However, values can be repeated; every occurrence of the same value gets stored in a different location.
Creating Arrays
Arrays can be created in several ways, each with a specific use case.
An empty array can be declared and initialized with the following syntax:
var arrayName = [Type]()
The above syntax is useful if elements are not supposed to be added yet, or the size of the array has not been determined.
Arrays can also be initialized with a given size and default values. For example, to store ten values for a Battleship game with a default location marker “U” (for “Unchecked”), an array can be initialized like this:
var alphaRow = [Character](repeating: "U", count: 10)// Creates: ["U", "U", "U", "U", "U", "U", "U", "U", "U", "U"]
Swift also allows different values to be assigned within the array directly at initialization. It will even perform type inferencing, enabling the code to be shortened.
// Type declaredvar statesVisited: [String] = ["NY", "CA", "TX", "FL"]// Type inferencedvar statesToVisit = ["ME", "ID", "NV", "AK"]
Note: If an array is declared as a constant, using
let
, then it is immutable: its size or values of its members cannot change. This is especially useful if the data stored is to be protected from being manipulated later on in the program.
Accessing Arrays
Arrays can be accessed using subscript notation. Since all arrays are zero-indexed, the first index in it will be arrayName[0]
, and the last element will be one less than the .count
property of the array.
var statesVisited: [String] = ["NY", "CA", "TX", "FL"]print("Yesterday I went to \(statesVisited[0]).")// Output: Yesterday I went to NY.var statesToVisit = ["ME", "ID", "NV", "AK"]print("Tomorrow I'm headed to \(statesToVisit[1])!")// Output: Tomorrow I'm headed to NV!
Attempting to access an element that doesn’t exist through subscript notation will result in a runtime error.
print("After Nevada, I'm going to head to \(statesToVisit[4]).")// Runtime Error: Index out of range
Values or elements of an array can be reassigned by accessing it through its index.
statesVisited[2] = "MI"// ["NY", "CA", "MI", "FL"]
Iterating Over Arrays
To iterate over the entire contents of an array, a for-in
loop is commonly used.
var topBabyNames = ["Liam", "Jackson"]for name in topBabyNames {print("\(name)is the coolest name.")}// Output:// Liam is the coolest name.// Jackson is the coolest name.
Note: Alternatively, the
.forEach()
method can be used to iterate over an array.
Array Properties, Methods, and Testing
Built-in array properties in the Swift Standard Library include .first
, .last
, and .randomElement
. These are used to access certain information within an array.
Note: If no values exist in the array, these properties will return
nil
.
print("The first and last states I've visited are \(statesVisited.first) and \(statesVisited.last), respectively.")// Output: The first and last states I've visited are NY and FL, respectively.print("To make life interesting, next week I'll fly to \(statesVisited.randomElement).")
Methods
There are many array manipulation methods available in the Swift Standard Library, including generic collection-based methods. Some of these include testing the contents of arrays (e.g. contains()
and .isEmpty
) while others can modify or manipulate the array entirely (e.g., map()
, reduce()
, and sort()
).
Below are some methods available for arrays:
Arrays
- .allSatisfy()
- Returns a boolean expression indicating whether every element of a sequence satisfies a given condition.
- .append()
- Adds an element to the end of an array.
- .contains()
- Returns true if an array contains a specified element, false otherwise.
- .filter()
- Returns a new array with only the elements that meet the condition stated in the filter.
- .first()
- Returns the first element of the array that satisfies the given condition.
- .firstIndex()
- Returns the index of the first element in an array that satisfies the given condition.
- .forEach()
- Loops over an array and performs some action on each element within it.
- .insert()
- Adds an element to a desired position of an array.
- .isEmpty
- Returns true if an array contains no elements, returns false if the array contains one or more elements.
- .map()
- Returns a new array with the results of calling a function for every element in the array.
- .randomElement()
- Returns a random element from an array.
- .reduce()
- Loops or iterates over every item in a sequence, combines them into one value using a specified closure, and returns the combined result.
- .remove()
- Removes an element from an array at a specified index.
- .removeFirst()
- Removes the first element in an array.
- .removeLast()
- Removes the last element in an array.
- .reverse()
- Reverses the elements within an array so that the first element becomes the last element and vice versa.
- .shuffle()
- Randomly shuffles the elements of an array.
- .sort()
- Sorts elements of any mutable collection in ascending order.
All contributors
- THE-Spellchecker
- Anonymous contributor
- Christine_Yang
- christian.dinh
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.