Arrays in Swift


In this Swift programming tutorial article, you will learn about arrays.

An array in Swift is a collection of similar types of data in an ordered list. You can also think of it as a list of similar types of data.

Note that, the same value can appear multiple times in an array at different positions.

Suppose, we need to record the name of 4 fruits. Using an array, we can assign it in a single variable instead of 4 variables:

Array diagram
Array diagram
Name of 4 fruits as array elements

Arrays are one of the common data types in the programming world and it is no different in the case of Swift. In the field of app development arrays are used widely to store similar types of data in a single variable or constant.

Creating an array in Swift

Now you will see how to create an array in Swift with examples.

First of all, let’s see the syntax of creating an array:

var ArrayName : [Type] = [ArrayElements]

Creating an integer type array

In an array of type integer, all the elements or items inside it will be integers.

Below is the Swift program to create an integer type array:

// Creating an array of integer type
var myNumbers : [Int] = [7, 32, 13, 9, 17]

print(myNumbers)

Output:

[7, 32, 13, 9, 17]

Creating a string type array

A string type array consists of string types elements only inside it

Below is the program for declaring string type array:

// Creating an array of string type
var fruitsArray : [String] = ["Strawberry", "Banana", "Apple", "Avocado"]

print(fruitsArray)

Output:

["Strawberry", "Banana", "Apple", "Avocado"]

Creating an empty array

An empty array is an array that has no elements inside it. That means it has 0 elements.

Below is the syntax of creating an empty array in Swift programming:

var ArrayName = [ArrayType]()

You can also store array in constant instead of variable:

let ArrayName = [ArrayType]()

An empty array of integer type:

Now let’s see an example below of creating an empty array of integer type:

//Declaring an empty array of integer type
var intArray = [Int]()

print(intArray)

Output:

[]

Empty array of string type:

Below is another example of creating an empty array, but this time it is an array of string type:

//Declaring an empty array of string type
var stringArray = [String]()

print(stringArray)

Output:

[]

Accessing an array element

We can easily access the array element by its index number. The index of a Swift array will always start with 0 and end in (N-1), where N is the number of elements in an array.

Array Indexing
Array indexing in Swift

Let’s see an example where we will access an element of an array in the Swift program:

var fruitsArray : [String] = ["Strawberry", "Banana", "Apple", "Avocado"]

print(fruitsArray[0])
print(fruitsArray[2])

Output:

Strawberry
Apple

From the above program, you can see that all we need is just to pass the index number of the particular element.

In the above array, if we want to get Avocado, then we can pass the index number of it. The index number of Avocado is 3. So we can get it by passing 3 in the array just like you can see below:

var fruitsArray : [String] = ["Strawberry", "Banana", "Apple", "Avocado"]

var getMyAvocado = fruitsArray[3]

Output:

Avocado

I hope you get some idea on the array and how it works in Swift language.

Find the length of an array

Now it’s time to learn how to get the size of an array in Swift. Here the size of an array refers to the number of elements present in the array. The number of array elements is the length of that particular array. So you have to count the number of elements.

In Swift, you can find the length of an array using the built-in count property. Below is the syntax of using this property with an array:

arrayName.count

Now see the example below to find the length of an array:

// Defining an Array
var myArray = [1,2,3,4]

// Get the length of the array
let arraySize = myArray.count
print(arraySize)

Output:

4