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:
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.
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]
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]
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"]
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]()
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:
[]
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:
[]
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.
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.
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