Updated April 6, 2023
Introduction to Swift array
Swift Array is the data structure that acts as the container for storing some values of the same type in an ordered list. Swift programming language does not allow much flexibility in terms of accessing the elements and manipulation rather follows quite a strict restriction in terms of Arrays declaration and manipulation. Various operations like adding, removing, or changing values are allowed in the Swift array, but invalid values may create some intrusion which needs to be handled carefully. Assigning a constant value in Swift array might create an immutable array, and contents cannot get changed.
Syntax
Various operations can be performed over a swift array, some of them with the syntax is described below:
- The syntax for Creating an Empty array containing no value with the initializer is represented as,
var sm_arr = [sm_data] ( )
where, sm_arr is the variable for storing the value and data.
sm_data represents the empty array initialization.
- The syntax for Accessing the elements from the swift array is represented as follows:
var sm_vr = Sm_Array[index]
where Sm_Array is used for representing the array, and index is used for accessing the elements.
How to declare an array in Swift?
Declaring an array in Swift is quite easy, and it then allows all the elements and values to get accessed and manipulated easily. There are two ways to declare an array which are as follows:
- One way is to initialize the variable with an empty array.
- Another way is to use the automatic type inference.
# To initialize the variable with an empty array:
The syntax is like the representation in the syntax section and is used accordingly.
For Example, > var m_var: Array<M_Class> = []
# To use the automatic type inference.
For Example, > var m_var = Array<M_Class>()
The basic advantage of this type of declaration can also be written as Array<M_Class> as [M_Class]
How to initialize an array in Swift?
Initialization of array in Swift is useful for performing operations and manipulations on the elements or items present in the Swift array concerning creating, accessing, and modifying. Also, it performs all the operations on top of it. Fetching of the index and returning the index required, while traversal in the index within the range, also plays an important role.
It is initialized in the following manner:
var arr_Title = [String]()
where arr_Title is the array variable, and [String] () is the array declaration where initialization with the string will be performed.
Note: Since a dynamic array is being used as part of initialization, it must append some value.
For Example:
var arr_Title = [String]()
arr_Title.append("strng_text")
print(arr_Title[1]);
A very important point to keep in mind is that Array initialization in the swift language is always performed after the array declaration. Since the declaration is the first step, otherwise initialization will be incomplete.
Examples
Here are the following examples mention below
Example #1
This program demonstrates the initialization of an empty array and printing an empty array as it is not assigned any value as shown in the output.
import Foundation
import Glibc
let emp_Int_Arr:[Int] = []
print(emp_Int_Arr)
Output:
Example #2
This program demonstrates the usage of string array by appending and adding some of the vegetables in the array and then performed a count on top of it as shown in the output.
import Foundation
import Glibc
var veggies = [String]()
veggies.append("Onion")
veggies.append("Potato")
veggies.append("Radish")
print(veggies.count)
print(veggies)
Output:
Example #3
This program demonstrates the string array of fruits which demonstrates the accessibility of fruits concerning the first and the last element index as shown in the output where the first index element is kiwi, and the last one is orange.
import Foundation
import Glibc
let fruits: [String] = ["kiwi", "apple", "orange"]
print(fruits[0])
print(fruits[fruits.count - 1])
Output:
Example #4
This program demonstrates the string array in swift, which is used for displaying the usage of enumerated function as shown in the output below.
import Foundation
import Glibc
var cars = ["bmw", "lamborgini", "jaguar"]
for (indx, val) in cars.enumerated()
{
print("\(indx) = \(val)")
}
Output:
Example #5
This program demonstrates the usage of array initializer, which is used for counting the number of elements in an array with respective sizes as shown in the output below.
import Foundation
import Glibc
var size_1: [Int] = [8, 6, 12,18,22]
size_1.append(26)
print("size_1: \(size_1)")
Output:
Example #6
This program demonstrates the usage of the Int array by using a for-in loop to get the unique number that is being appended in the swift array, as shown in the output below.
import Foundation
import Glibc
var uniq_no = [Int]()
uniq_no.append(15)
uniq_no.append(13)
uniq_no.append(20)
for uniq_no in uniq_no
{
print(uniq_no)
}
Output:
Example #7
This program demonstrates the usage of subscript syntax to check for the accessing of elements in Swift to make the index lie with the runtime check.
import Foundation
import Glibc
let int_arr_0 = [20, 18, 16, 23]
print(int_arr_0 [-1])
Output:
Example #8
This program demonstrates the swift array to be present in the reverse order and makes the elements represented similarly as shown in the output below as per the indexing.
Note: Basically, the array returns elements of an array in reverse order.
import Foundation
import Glibc
var int_ar_2 = [20,15,2,18]
let revrs_arr = Array(int_ar_2.reversed())
print(revrs_arr)
Output:
Conclusion
Swift Array plays a pivotal role in a swift programming language as it allows programmers to work according to the requirement to keep the elements in place and ordered. Many operations like creation, addition, deletion, removal, and many operations can be performed on top of it for manipulating the data structure array properly.
Recommended Articles
We hope that this EDUCBA information on “Swift array” was beneficial to you. You can view EDUCBA’s recommended articles for more information.