”;
Arrays are used to store ordered lists of values of the same type. Swift puts strict checking which does not allow you to enter a wrong type in an array, even by mistake. If you assign a created array to a variable, then it is always mutable, which means you can change it by adding, removing, or changing its elements; but if you assign an array to a constant, then that array is immutable, and its size and contents cannot be changed. If you try to change it, then the compiler will throw an error.
An array can store duplicate values in different positions. Each element in the array has an index value starting from 0 so that we can access and modify them using that index.
Creating Arrays in Swift
We can create an array by specifying the type of the array explicitly.
Syntax
Following is the syntax for creating an array −
var someArray : [Type] = []
We can also create an array without specifying its type. In this case, the compiler will automatically get the type of the array based on the assigned value.
var someArray = [value1, value2, value3]
If you need to create an array with a single value (by repeating it), you can do so using the Array() initializer.
var someInts = Array(repeating: "Swift", count: 4)
Example
In the following example we are creating 3 different arrays using all the above discussed syntaxes −
import Foundation // Defining an array by specifying the type var arr1:[Int] = [11, 44, 55, 77, 88, 22] print("Contents of arr1 :", arr1) // Defining an array without specifying the type var arr2 = [101, 404, 550, 770, 880, 222] print("Contents of arr2 :", arr2) // Defining an array with a single value var arr3 = Array(repeating: "Tutorialspoint", count: 3) print("Contents of arr3 :", arr3)
Output
The above program produces the following result −
Contents of arr1 : [11, 44, 55, 77, 88, 22] Contents of arr2 : [101, 404, 550, 770, 880, 222] Contents of arr3 : ["Tutorialspoint", "Tutorialspoint", "Tutorialspoint"]
Modifying and Accessing Arrays in Swift
In an array, every element has its own index value starting from 0. So to retrieve a value from an array, pass the index of the value we want to retrieve within square brackets immediately after the name of the array in the subscript syntax.
Syntax
Following is the syntax for accessing and modifying array −
arrayName[indexValue]
Here, the index starts from 0 which means the first element can be accessed using the index as 0, the second element can be accessed using the index as 1 and so on.
Example
import Foundation // Defining and initializing an array var someArr:[Int] = [11, 44, 55, 77, 88, 22] // Accessing the element present at index 3 // Using subscript syntax print("Array element:", someArr[3])
Output
When the above code is compiled and executed, it produces the following result −
Array element: 77
Using subscript syntax, we can also modify the elements of the array by assigning new value to the existing index.
Example
import Foundation // Defining and initializing an array var someArr:[Int] = [11, 44, 55, 77, 88, 22] // Modifying the array element present at index 3 // Using subscript syntax someArr[3] = 33 print("Modified Array element:", someArr[3])
Output
When the above code is compiled and executed, it produces the following result −
Modified Array element: 33
Adding a New Element in an Array in Swift
We are allowed to add new elements to the existing array using the following methods.
Using append() Method
It adds a new element at the end of the specified array.
Example
import Foundation // Defining and initializing an array var someArr:[Int] = [43, 32, 11] // Appending new element using append() method someArr.append(34) someArr.append(60) print("Updated Array", someArr)
Output
When the above code is compiled and executed, it produces the following result −
Updated Array [43, 32, 11, 34, 60]
Using the addition assignment operator (+=)
We can also add a new item at the end of an array using the addition assignment operator.
Example
import Foundation // Defining and initializing an array var someArr:[Int] = [43, 32, 11] // Adding new element using += operator someArr += [30] someArr += [90] print("Updated Array", someArr)
Output
When the above code is compiled and executed, it produces the following result −
Updated Array [43, 32, 11, 30, 90]
Iterating Over an Array in Swift
Iterating over an array is the fundamental and most commonly used operation in programming. It allows the developer to access and process individual elements of the specified array. In Swift, we can iterate over an array using the following methods −
Using for-in loop
We can use a for-in loop to iterate over the entire set of values in an array. It is the easiest and cleanest method to access and process each element of the given array sequentially.
Example
import Foundation // Defining and initializing an array var someArr:[Int] = [3, 56, 12, 4, 23, 5, 6, 7, 8] print("Array Elements:") // Iterating over the array using a for-in loop for x in someArr{ print(x) }
Output
When the above code is compiled and executed, it produces the following result −
Array Elements: 3 56 12 4 23 5 6 7 8
Using the enumerated() function with for-in loop
We can also use the enumerated() function along with the for-in loop, it returns the index of the item along with its value.
Example
import Foundation // Defining and initializing an array var someArr:[Int] = [3, 56, 23, 34, 5, 78, 9] print("Array Elements with their index value:") // Iterating over the array using for-in loop along with enumerated() function for (index, element) in someArr.enumerated() { print("Value at index = (index) is (element)") }
Output
When the above code is compiled and executed, it produces the following result −
Array Elements with their index value: Value at index = 0 is 3 Value at index = 1 is 56 Value at index = 2 is 23 Value at index = 3 is 34 Value at index = 4 is 5 Value at index = 5 is 78 Value at index = 6 is 9
Using while loop
We can also iterate over the element of the given array using a while loop. It provides explicit control over the iteration. It can be useful where a fine-grained control is required over the iteration.
Example
import Foundation // Defining and initializing an array var someArr:[Int] = [3, 56, 23, 34, 5, 78, 9] print("Array Elements:") var indexVal = 0 // Iterating over the array using while loop while indexVal < someArr.count { print(someArr[indexVal]) indexVal += 1 }
Output
When the above code is compiled and executed, it produces the following result −
Array Elements: 3 56 23 34 5 78 9
Using forEach() Function
Swift provides a pre-defined function named forEach() to iterate over the given array. It is useful to perform simple operations on the individual elements of the given array without iterating explicitly.
Example
import Foundation // Defining and initializing an array var someArr:[String] = ["Mona", "Pihu", "Mayank", "Sumit"] print("Array Elements:") // Iterating over the array using forEach() function someArr.forEach { names in print(names) }
Output
When the above code is compiled and executed, it produces the following result −
Array Elements: Mona Pihu Mayank Sumit
Adding Two Arrays in Swift
We can use the addition operator (+) to add two arrays of the same type which will yield a new array with a combination of values from the two arrays.
Syntax
Following is the syntax for adding two arrays −
var result = Array1 + Array2
Example
import Foundation // Defining and initializing an array var someArr1:[Int] = [11, 44, 55, 77, 88, 22] var someArr2:[Int] = [10, 40, 50, 70, 80, 20] // Adding two arrays of the same type // Using + operator var resultantArray = someArr1 + someArr2 print("Concatenated Array: ", resultantArray)
Output
When the above code is compiled and executed, it produces the following result −
Concatenated Array: [11, 44, 55, 77, 88, 22, 10, 40, 50, 70, 80, 20]
The count Property in Swift
The count property is used to count the total number of elements present in the specified array.
Syntax
Following is the syntax of the count property −
Array.count
Example
import Foundation // Defining and initializing arrays var someArr1:[Int] = [3, 56, 12, 4, 23, 5, 6, 7, 8] var someArr2:[String] = ["Blue", "Black", "Green", "Yellow"] var someArr3:[Int] = [] // Counting the total number of elements present // in the given array // Using count property print("Total number of elements present in someArr1 = (someArr1.count)") print("Total number of elements present in someArr2 = (someArr2.count)") print("Total number of elements present in someArr3 = (someArr3.count)")
Output
When the above code is compiled and executed, it produces the following result −
Total number of elements present in someArr1 = 9 Total number of elements present in someArr2 = 4 Total number of elements present in someArr3 = 0
The empty Property in Swift
The empty property is used to check whether the given array is empty or not. This property will return true if the given array is empty. If the given array contains some element, then this property will return false.
Syntax
Following is the syntax of the empty property −
array.isEmpty
Example
import Foundation // Defining and initializing arrays var someArr1:[Int] = [2, 5, 66, 44, 32, 2, 12] var someArr2:[String] = ["Blue, Black"] var someArr3:[Int] = [] // Checking if the given arrays are empty or not // Using isEmpty property print("Is someArr1 is empty? = (someArr1.isEmpty)") print("Is someArr2 is empty? = (someArr2.isEmpty)") print("Is someArr3 is empty? = (someArr3.isEmpty)")
Output
When the above code is compiled and executed, it produces the following result −
Is someArr1 is empty? = false Is someArr2 is empty? = false Is someArr3 is empty? = true
”;