Array reference guide for Swift

Swift makes creating and modifying arrays really easy.  When you create your array, you don’t have to initialize it with a certain type, if all of your given values are the same type.  Example: If you create an array and all of the initial values are Strings, Swift automatically infers that the array will be a string array.

This guide will be updated in the future as we dive deeper into Swift, be sure to bookmark this page and check back from time to time.

This guide has been updated to work with Swift 3 — If you’re using an older version of Swift, some code samples may not work.

Creating a mutable array
Creating an immutable array
Creating an empty array
Creating an array to hold a specific type
Counting the number of items in an array
Check if an array is empty
Add an item to the end of an array
Modify a value inside an array
Modifying multiple values inside an array
Removing a value from a specific index
Removing the last value from an array
Removing all items
Iterating over an array
Revsersing the values in an array

 


Creating a mutable array

Notice that the beginning of the line begins with var, which means variable and allows the contents of the array to be changed in the future.

var cityArray = ["Portland","San Francisco","Cupertino"]


Creating an immutable array

Notice that the beginning of the line begins with let, which means constant and means the contents can not be changed in the future.

let cityArray = ["Portland","San Francisco","Cupertino"]


Create an empty array
If you need to create an empty array, you must initialize it with the type of object that will be added later.

var animalArray = [String]()
//This array will only allow strings.

 

var animalAgeArray = [Int]()
//This array will only hold integers, nothing else.


Creating an array to hold a specific type

This variable array will only hold string objects, nothing else.

var cityArray: [String] = ["Portland","San Francisco","Cupertino"]

This variable array will only hold integers, nothing else.

var numberArray:[Int] = [1,3,4]


Counting number of items inside an array
Use the read-only count property to count the number of items in an array.

var cityArray: [String] = ["Portland","San Francisco","Cupertino"]
let count = cityArray.count
//count = 3


Check if array is empty
Use the Boolean isEmpty as a quick way to determine if an array is empty.

var cityArray: [String] = ["Portland","San Francisco","Cupertino"]

if cityArray.isEmpty {
    print("Empty") 
}else{
    print("Not Empty")
}

//prints "Not Empty"


Add an item to the end of an array

var cityArray: [String] = ["Portland","San Francisco","Cupertino"]
cityArray.append("Seattle")

//The array now contains 4 items


Add an array of items to an existing array
Use the addition assignment operator (+=) to quickly add an array of items.

var cityArray: [String] = ["Portland","San Francisco","Cupertino","Seattle"]
cityArray += ["Vancouver", "Los Angeles", "Eugene"]

//The array now contains 7 items

Alternately, if you have already created an array:

var cityArrayA: [String] = ["Portland","San Francisco","Cupertino","Seattle"]
var cityArrayB: [String] = ["Vancouver", "Los Angeles", "Eugene"]
cityArrayA += cityArrayB;

//cityArrayA now contains 7 items.




Modify a value inside an array
Pass the index of the value you wish to change, along with the new value.

var cityArray: [String] = ["Portland","San Francisco","Cupertino","Seattle"]
cityArray[0] = "Portland, Oregon"

//cityArray is now ["Portland, Oregon", "San Francisco", "Cupertino", "Seattle"]



Modifying multiple values inside an array
Swift makes it easy to change multiple values inside an array at once. Using the subscript syntax, you provide the index of the items you wish to modify as well as the new additions.

var cityArray = ["Portland, Oregon","San Francisco","Cupertino","Seattle"]
cityArray[1...3] = ["San Francisco, California","Cupertino, California","Seattle, Washington"]

//We replaced the values at indexes 1,2 & 3 and now cityArray contains:
 ["San Francisco, California", "Cupertino, California", "Seattle, Washington"]

Note: The amount of replacement items does not have to equal the length of the items you are replacing
Example:

var cityArray: [String] = ["Portland, Oregon","San Francisco","Cupertino","Seattle"]
cityArray[1...3] = ["San Francisco, California","Cupertino, California"]
//cityArray contains: ["Portland, Oregon", "San Francisco, California", "Cupertino, California"]

Even though we tell the cityArray to replace indexes 1 through 3, we only give 2 values back to the array.


Removing a value from a specific index

var animalArray: [String] = ["Dog", "Cat", "Fish", "Owl", "Beaver"]
animalArray.remove(at:4)

//animalArray now contains: ["Dog", "Cat", "Fish", "Owl"]

Alternately, if you wish to save the value before you remove it:

let stringAnimal = animalArray.remove(at:4)
//Saves "Beaver" to the string constant stringAnimal and also removes it from the array.


Removing the last value of an array
To avoid having to call count on an array, Swift allows developers to simply call removeLast to remove the last item in an array.

var animalArray: [String] = ["Dog", "Cat", "Fish", "Owl" ]
animalArray.removeLast()

//animalArray now contains 3 items, "Owl" has been removed.

Again, if you wish to save the value before you remove it:

let stringAnimal = animalArray.removeLast()
//Saves "Owl" (which was the last item in the array) to the string constant stringAnimal and also removes it from the array.


Removing all items
To remove all items from an array, use the removeAll method.

var animalArray: [String] = ["Dog", "Cat", "Fish", "Owl" ]
animalArray.removeAll()
//animalArray is now empty.

Keeping the capacity of the array

animalArray.removeAll(keepCapacity: true)
//animalArray is now empty but the capacity is kept at 4.



Iterating over an array
Simple for in Loop

for animal in animalArray {

    print(animal)

}

//Prints each animal name into the console.

for in Loop + Enumeration

If you want to have the index of the item as well, you must use the enumerate function with your loop. The enumerate function returns a Tuple for each value in the array.

for(index,animal) in animalArray.enumerate() {
    
    print("The \(animal) is at index:\(index)")
    
}

//Output: 
//The Dog is at index:0
//The Cat is at index:1
//The Fish is at index:2
//The Owl is at index:3


Reversing the values in an array

var animalArray: [String] = ["Dog", "Cat", "Fish", "Owl" ]
animalArray.reverse()

//The array values are now ["Owl", "Fish", "Cat", "Dog"]

Create a new array from the reversed values

var animalArray: [String] = ["Dog", "Cat", "Fish", "Owl" ]
let newArray = animalArray.reverse()
//The newly created array contains ["Owl", "Fish", "Cat", "Dog"]