Swift version: 3.0 | View all code snippets in this post on Github

Declaring Arrays

Arrays hold a collection of values with the same type. They can be declared using the bracket shorthand syntax, commonly referred to as an array literal:

var starters = ["Bulbasaur", "Charmander", "Squirtle"]

The type of values in an array can be inferred as in the example above (string).
It can also be explicitly set using the [Type] syntax:

var legendaries: [String] = []

An array can only hold values of the same type:

var starters = ["Bulbasaur", 2, "Squirtle"]
// -> Error

You can create an array of a certain length and give its elements a default value like so:

var items = [String](repeating: "Potion", count: 3)
// ["Potion", "Potion", "Potion"]

Inspecting Arrays

You can get the number of values in an array using count:

// -> 3

To check whether an array is empty, you can use the isEmpty() function:

// -> false

To get the first or last value1 in an array, use the first() and last() methods respectively:

// -> "Bulbasaur"
// -> "Squirtle"

Use the contains() method to check if the array contains a given value:

let isBulbasaurStarter = starters.contains("Bulbasaur")
// -> true
let isPidgeotStarter = starters.contains("Pidgeot")
// -> false

Beware that this won’t work if the value being checked is of a different type than the one the array holds:

// -> Error

Getting Array Values

Use subscript syntax to access the value stored at a given index:

let pokemon = starters[0]
// -> "Bulbasaur"

Note that the first item has an index of 0.

Adding Array Values

To add a new value, use the append() function:

// -> ["Articuno"]
// -> ["Articuno", "Zapdos"]
// -> ["Articuno", "Zapdos", "Moltres"]

Concatenating Arrays

You can concatenate two arrays using the += operator:

starters += legendaries
// -> ["Bulbasaur", "Charmander", "Squirtle", "Articuno", "Zapdos", "Moltres"]

Mutating Arrays

In order to mutate the values included in an array, it needs to be declared as a variable using the
keyword var; constant arrays are immutable:

let otherLegendaries: [String] = []
// -> Error

Inserting Values

To insert a value at a given index, use insert:

starters.insert("Pikachu", at: 3)
// -> ["Bulbasaur", "Charmander", "Squirtle", "Pikachu", "Articuno", "Zapdos", "Moltres"]

Updating Array Values

To update an existing value, use the subscript syntax:

starters[5] = "Mewtwo"
// -> ["Bulbasaur", "Charmander", "Squirtle", "Pikachu", "Articuno", "Mewtwo", "Moltres"]

Deleting Array Values

To delete a value at a specific index, use remove(at:):

starters.remove(at: 5)
// -> ["Bulbasaur", "Charmander", "Squirtle", "Pikachu", "Articuno", "Moltres"]

Use removeLast() to, remove the last value:

// -> ["Bulbasaur", "Charmander", "Squirtle", "Pikachu", "Articuno"]

You can also remove multiple values by using removeSubrange():

// -> ["Bulbasaur", "Charmander", "Squirtle"]
  1. In practice, what you get is an optional value, which means it can be nil, for example when the array is empty. More on optionals later.