To get the length of an array in Swift, you can use the count
property of the array.
In this example,
numbers
with some values.count
property of the array object numbers
, and store the returned value in length
variable.Array.count
property returns the length of this array.length
to standard output.let numbers = [1, 2, 3, 4, 5]
let length = numbers.count
print("Length of array is: \(length)")
Length of array is: 5
In this example,
names
with some values.count
property of the array object names
, and store the returned value in length
variable.Array.count
property returns the length of this array.length
to standard output.let names = ["Alice", "Bob", "Charlie", "David", "Eve"]
let length = names.count
print("Length of array is: \(length)")
Length of array is: 5
In this tutorial, we learned How to Get Array Length in Swift language with well detailed examples.