In swift, arrays are useful to store the same data type of multiple values in an ordered manner. In array, we can store the same value in multiple times at different positions based on our requirements.
In swift arrays, we can store any kind of elements like either strings or integers or other element types based on our requirements.
We can use arrays in swift programming language whenever we want to hold multiple values of single data type in one variable.
Following is the pictorial representation of arrays in a swift programming language.
If you observe the above image we are storing values in swift arrays based on index positions in an ordered manner.
By using index we can access arrays elements and perform update or delete operations on array elements based on our requirements in swift programming language.
In swift, we can create arrays by surround a list of comma-separated values with square brackets.
The following are the different ways of defining or declaring arrays in swift programming language.
The simple way of defining array elements with comma-separated values surrounded by square brackets.
// An array of integer elements
var numbers = [1, 2, 3, 4]
// An array of string elements
var names = ["Suresh", "Rohini", "Praveen", "Tulasi", "Sateesh"]
Following is another way of creating an array with data types in an array declaration.
var nums: Array<Int> = [1,2,3]
print(nums)
// It prints [1, 2, 3]
Following are the different ways of create an empty arrays with data type in an array declaration.
var intvals = Array<Int>()
var intarr = [Int]()
If we want to define an array with fixed number of default values then by using following approach we can define default values.
var numofvals = [Int](count: 10, repeatedValue: 1)
print(numofvals)
// It prints [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
When we run above code snippet in swift playground we will get [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] result.
Now we will see how to access array elements in a swift programming language with examples.
In swift, we can access array elements by using index positions. Generally in arrays index position will start from zero (0).
Following is the example of accessing array elements using index positions.
var names = ["suresh", "rohini", "praveen", "sateesh"]
print(names)
print(names[0])
print(names[1])
print(names[2])
print(names[3])
When we run above example we will get result like as shown below.
[suresh, rohini, praveen, sateesh]
suresh
rohini
praveen
sateesh
We can access array elements by using a for-in loop in a swift programming language. Following is the example of accessing array elements using the for-in loop.
var names = ["suresh", "rohini", "praveen", "sateesh"]
for name in names {
print(name)
}
When we run above example we will get result like as shown below
suresh
rohini
praveen
sateesh
In swift, we have properties called first and last to access the first and last elements of array elements.
Following is the example of accessing the first & last elements of an array.
var names = ["suresh", "rohini", "praveen", "sateesh"]
var firstname = names [0]
print(firstname)
print(names.first, names.last)
If you observe the above example first we element we can access either by using index position “0” or “first” property. When we run the above example we will get a result like as shown below.
suresh
suresh, sateesh
This is how we can access elements from an array based on our requirements in swift programming language.
By using isEmpty property we can check whether the given array contains elements or not in a swift programming language.
Following is the example of using isEmpty property to check whether the given array contains elements or not.
var names = ["suresh", "rohini", "praveen", "sateesh"]
if names.isEmpty {
print("Array is Empty")
} else {
print("Array Contains Elements")
}
If you observe above example we are checking an array contains elements or not using isEmpty property.
When we run above program in swift playground that will return a result like as shown below
This is how we can check whether the given array contains elements or not in swift programming language based on our requirements.
In swift by using count property we can get the count or number of elements that exist in an array.
Following is the example of using count property to get the number of elements that exists in an array.
var names = ["suresh", "rohini", "praveen", "sateesh"]
if names.count > 0 {
print("Array Contains \(names.count) Elements")
} else {
print("No Elements")
}
If you observe above example we are checking whether given array contains elements or not using count property.
When we run the above program in the swift playground that will return a result like as shown below
This is how we can get the number of elements that exist in an array in a swift programming language based on our requirements.
In swift we can add elements to an array by using built-in append() method or += operator based on our requirements.
Following is the example of adding elements to an existing array in a swift programming language.
var names = ["suresh", "rohini", "praveen", "sateesh"]
names.append("sudheer")
names += ["madhav"]
print(names)
If you observe above example we are adding new elements to an “names” array using append() method and += operator.
When we run the above program in swift playground that will return a result like as shown below.
Suppose if we want to insert elements at a particular index position of an array then by using the insert() method we can insert elements in an array based on our requirements.
Following is the example of inserting new elements at a particular position in an array using the insert() method in the swift programming language.
var names = ["suresh", "rohini", "praveen", "sateesh"]
names.insert("madhav", atIndex: 2)
print(names)
If you observe above example we are inserting new element at the index position of “3” using insert() method.
When we run the above program in a swift playground that will return a result like as shown below
This is how we can insert or append elements to an existing array in a swift programming language based on our requirements.
In Swift, we can easily delete or remove the elements from an array by using removeLast() and removeAtIndex() methods based on our requirements.
Following is the example of removing the last element of an array by using removeLast methods.
var names = ["suresh", "rohini", "praveen", "sateesh"]
names.removeLast()
print(names)
If you observe above example we are deleting last element of an array by using “removeLast” method.
When we run the above example we will get a result like as shown below
Now we will see how to delete array elements based on index position by using removeAtIndex method.
Following is the example of deleting array elements using removeAtIndex method.
var names = ["suresh", "rohini", "praveen", "sateesh"]
names.removeAtIndex(2)
print(names)
If you observe above example we are deleting array element at the index position of 2 by using “removeAtIndex” method.
When we run the above example we will get a result like as shown below
This is how we can delete elements in an array based on our requirements in swift programming language.
In swift, we can easily update array elements by assigning new values based on index positions.
Following is the example of updating an array element at a particular index position in the swift programming language.
var names = ["suresh", "rohini", "praveen", "sateesh"]
names[2] = "madhav"
print(names)
If you observe above example we are updating an array element at the index position of 2.
When we run the above example we will get a result like as shown below
This is how we can update array elements in swift programming language based on our requirements.
By using for-in loop we can iterate or loop through elements in an array based on our requirements in swift programming language.
Following is the example of iterating over elements in an array by using the for-in loop.
var names = ["suresh", "rohini", "praveen", "sateesh"]
for name in names {
print(name)
}
When we run above example we will get result like as shown below
suresh
rohini
praveen
sateesh
Following is another example of accessing array elements using the for-in loop with a numeric bullet for each item.
var names = ["suresh", "rohini", "praveen", "sateesh"]
for (index, name) in names.enumerate() {
print("\(index + 1). \(name)")
}
When we run above example we will get result like as shown below
1.suresh
2.rohini
3.praveen
4.sateesh
This is how we can loop through or iterate over array elements in a swift programming language based on our requirements.