Apple Compatibility

Swift Array Operations – Create, Access and Modify

The reason behind SWIFT is to develop the IOS Applications. It is developed by Apple which is a General purpose programming language.

Python (just like Java) and other languages Swift support Data Types, operators, Looping statements, Conditional statements, Collections, and it can be possible to implement Object Oriented Programming.

In this Swift guide, we will see what an Array collection is in Swift and how to create, access, and modify elements in an Array collection.

Before going to Array creation, we will see what an Array is and how to declare and initialize it.

Swift Array

Array in Swift is a Collection that stores multiple elements with the same data type.

In Swift, we have to specify the datatype to represent the data type of Array while creating an array.

There are two ways to declare an array in Swift.

Array Declaration

The first way that we can declare an array is by using square brackets – [].

Here, we will use the let keyword to define an array. Then, we will specify the Array name that represents the name of an array followed by data type.

Syntax:

let swift_array:[datatype] = []  

let swift_array:[Int] = []

 
Here, you can see that we declared an empty array named – swift_array with datatype – Int.
Int represents the Integer that stores only integer type values.

The second way to declare an array is by using the Array() function.

Here, we will use the let keyword to define an array. Then, we will specify the Array name that represents the name of an array followed by data type.

Syntax:

let swift_array:Array<datatype> = Array()

 
So, these are the two possible ways to declare an Array in Swift.

Example:

Here, we are creating two empty swift arrays named – swift_array1 and swift_array2 and displaying them.

//create swift_array
let swift_array1:[Int] = []  

//display swift_array
print(swift_array1)

//create swift_array
let swift_array2:Array<Int> = Array()

//display swift_array
print(swift_array2)

 
Output:


As the arrays are empty, empty arrays are returned.

Array Creation

Now, we will see how to create an array in swift with some elements of Integer type.

We can directly specify elements in a Swift array without specifying data type also.

Syntax:

let swift_array:[datatype] = [element1,element2,...........]  

(OR)
let swift_array = [element1,element2,...........]

 
Where swift_array is the array name.

Example:

Let’s create two swift arrays with five Integer values and display them.

//create swift_array with values
let swift_array1:[Int] = [10,20,30,40,50]  

//display swift_array
print(swift_array1)

//create swift_array with values without specifying the data type
let swift_array2 = [10,20,30,40,50]  

//display swift_array
print(swift_array2)

 
Output:


So, we created arrays in two ways. swift_array1 is created by specifying the data type – Int and swift_array2 is created without specifying the datatype.

Accessing elements from Swift Array

If we want to display the entire swift array, we use print() statement. Now, if you want to get only a particular element from the array, index positions are used.

Index starts with 0. By using index, it is possible to access elements from the swift array.

Syntax:

swift_array[index]

 
Where swift__array is an input swift array and index represents an integer value that specifies the index position.

Example 1:

Now, we will create an Integer array named – swift_array1 with 10 elements and access the elements.

//create swift_array with values
let swift_array1:[Int] = [10,20,30,40,50,1,2,3,4,5]  

//display swift_array
print("Actual Swift Array: \(swift_array1)")

//access 4th element
print("Element present at Index - 3 is \(swift_array1[3]).")

//access 8th element
print("Element present at Index - 7 is \(swift_array1[7]).")

//access 1st element
print("Element present at Index - 0 is \(swift_array1[0]).")

 
Output:


So elements with respect to index positions were returned.

Example 2:

Now, we will create a String array named – swift_array1 with 4 elements and access the elements.

//create swift_array with strings
let swift_array1:[String] = ["swift1","swift2","swift3","swift4"]  

//display swift_array
print("Actual Swift Array: \(swift_array1)")

//access 1st element
print("Element present at Index - 0 is \(swift_array1[0]).")

//access 3rd element
print("Element present at Index - 2 is \(swift_array1[2]).")

 
Output:


So the string elements with respect to index positions were returned.

Modifying elements in Swift Array

It is possible to modify the particular elements in the swift array or entire swift array using index positions.

Index starts with 0. At particular index position, we will assign the new element.

If you want to modify the entire array, you can pass the new elements in the new array and no need of Index is provided.

Syntax:

For particular element – swift_array[index]=new_element
For the entire Array – swift_array=[new_element,…..]

Where, swift_array is an input swift array and new_element is the element that replaces the existing element in the swift array.

Example 1:

We created four strings and updated some strings using Index position.

//create swift_array with strings
var swift_array1:[String] = ["swift1","swift2","swift3","swift4"]  

//display swift_array
print("Actual Swift Array: \(swift_array1)")

//update 1st element to 'SW-1'
swift_array1[0]="SW-1"

//update 3rd element to 'SW-3'
swift_array1[2]="SW-3"

//display swift_array
print("Updated Swift Array: \(swift_array1)")

 
Output:


So, SW-1 replaces swift1 and SW-3 replaces swift3.

Example 2:

We created five integers and updated some values using Index position.

//create swift_array with integers
var swift_array1:[Int] = [34,56,53,23,56]  

//display swift_array
print("Actual Swift Array: \(swift_array1)")

//update 1st element to 1
swift_array1[0]=1

//update 3rd element to 3
swift_array1[2]=3

//update 5th element to 5
swift_array1[4]=5

//display swift_array
print("Updated Swift Array: \(swift_array1)")

 
Output:


So, 1 replaces 34, 3 replaces 53 and 5 replaces 56.

Example 3:

Now, we will try to update the entire array.

Here, we created two Arrays that hold strings and integers and update all the elements in both the swift arrays.

//create swift_array with integers
var swift_array1:[Int] = [34,56,53,23,56]  

//create swift_array with strings
var swift_array2:[String] = ["swift1","swift2"]  

//display swift_array
print("Actual Swift Integer Array: \(swift_array1)")

//update the entire array
swift_array1=[100,200,300,400,500]

//display swift_array
print("Updated Swift Array: \(swift_array1)")

//display swift_array
print("Actual Swift String Array: \(swift_array2)")

//update the entire array
swift_array2=["Hello","Linuxhint"]

//display swift_array
print("Updated Swift Array: \(swift_array2)")

 
Output:


The values in the Integer Array (swift_array1) are updated to 100,200,300,400, and 500. The values in the String Array (swift_array2) are updated to “Hello”,”Linuxhint”.


 

Conclusion

In this Swift Array guide, we saw how to create an Array in Swift. We can do it using the Array() function or using []. Next, we explored how to access elements from an Array through Index positions and it is possible to modify the elements using the index positions also. We discussed different examples to access and modify particular or all elements in the array.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain