by Sai gowtham

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)

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
130,921 students enrolled
48.5 hours of video content
View Course