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.
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.
var arr_Title = [String]()
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.
Here are the following examples mention below
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.
let emp_Int_Arr:[Int] =  print(emp_Int_Arr)
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.
var veggies = [String]()
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.
let fruits: [String] = ["kiwi", "apple", "orange"] print(fruits)
print(fruits[fruits.count - 1])
This program demonstrates the string array in swift, which is used for displaying the usage of enumerated function as shown in the output below.
var cars = ["bmw", "lamborgini", "jaguar"] for (indx, val) in cars.enumerated()
print("\(indx) = \(val)")
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.
var size_1: [Int] = [8, 6, 12,18,22] size_1.append(26)
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.
var uniq_no = [Int]()
for uniq_no in uniq_no
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.
let int_arr_0 = [20, 18, 16, 23] print(int_arr_0 [-1])
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.
var int_ar_2 = [20,15,2,18] let revrs_arr = Array(int_ar_2.reversed())
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.
This is a guide to Swift array. Here we discuss How to declare and initialize an array in Swift and Examples along with the outputs. You may also have a look at the following articles to learn more –