How to get the length of an array in Swift
In this tutorial, we are going to learn about how to find the length of an array in Swift.
The length of an array means, the total number of elements present in the given array.
Getting the array length
To get the length of an array, we can use the built-in count
property in Swift language.
Here is an example:
var arr = ["a", "b", "c", "d"]
// Get the length of the array
let length = arr.count
print(length)
Output:
4
Similarly, we can also use for loop to calculate the length of an array in swift.
var arr = ["a", "b", "c", "d"]
var length = 0
for _ in arr{
length += 1
}
print(length)