Swift Array Check if an index exists


In Swift, there are several ways to check whether an index exists in an array. You can use startIndex, endIndex, the indices property, and the count property. In this article, you will see some examples of checking an index.

Example 1: Using FirstIndex & EndIndex

You can check if a specific index exists in a Swift array by comparing the index against the startIndex and endIndex properties of the array. Here's an example.

import Foundation
let inputArray = [1, 2, 3, 4, 5]
let targetIndex = 3
if targetIndex >= inputArray.startIndex && targetIndex < inputArray.endIndex {
   print("Index \(targetIndex) exists in the array \(inputArray)")
} else {
   print("Index does not exist in the array")
}

Output

Index 3 exists in the array [1, 2, 3, 4, 5]

In this example, we first establish an index that we wish to verify and an array named inputArray. Next, we compare targetIndex to the startIndex and endIndex attributes of inputArray using an if statement. TargetIndex exists in the array if it is higher than or equal to startIndex, less than endIndex, and less than targetIndex; in that case, a message to that effect is printed. If not, a message stating that the index doesn't exist in the array is printed.

Example 2: Using the Indices Property

import Foundation
let inputArray = [1, 2, 3, 4, 5]
let targetIndex = 3
if inputArray.indices.contains(targetIndex) {
   print("Index \(targetIndex) exists in the array \(inputArray)")
} else {
   print("Index does not exist in the array")
}

Output

Index 3 exists in the array [1, 2, 3, 4, 5]

In this example, we use the indices property of the array to check if targetIndex exists. The indices property returns a range of all valid indices for the array. We can use the contains() method to check if targetIndex is within that range.

Example 3: Using Optional Binding

import Foundation
let inputArray = [1, 2, 3, 4, 5]
let targetIndex = 3
if let _ = inputArray.indices.firstIndex(of: targetIndex) {
   print("Index \(targetIndex) exists in the array \(inputArray)")
} else {
   print("Index does not exist in the array")
}

Output

Index 3 exists in the array [1, 2, 3, 4, 5]

In this example, we use the firstIndex() method of the array's indices property to get the index of the element that matches targetIndex. If such an index exists, the method returns it, and we can use optional binding to print a message saying that the index exists. If the method returns nil, then the index does not exist in the array.

Example 4: Using the Count Property

import Foundation
let inputArray = [1, 2, 3, 4, 5]
let targetIndex = 3
if targetIndex < inputArray.count {
   print("Index \(targetIndex) exists in the array \(inputArray)")
} else {
   print("Index does not exist in the array")
}

Output

Index 3 exists in the array [1, 2, 3, 4, 5]

In this example, we check if targetIndex is less than the count property of the array. If it is, then the index exists in the array and we print a message saying so. If it is not, then the index does not exist in the array. Note that we do not need to check if targetIndex is greater than or equal to 0, because the count property is always non-negative.

Example 5: Using a Guard Statement

import Foundation
func checkIndex() {
   let inputArray = [1, 2, 3, 4, 5]
   let targetIndex = 3
   guard targetIndex < inputArray.count else {
      print("Index does not exist in the array")
      return
   }    
   print("Index \(targetIndex) exists in the array \(inputArray)")
}
checkIndex()

Output

Index 3 exists in the array [1, 2, 3, 4, 5]

In this example, we use a guard statement to check if targetIndex is less than the count property of the array. If it is, we print a message saying that the index exists. If it is not, we print a message saying that the index does not exist and return from the current scope.

Conclusion

In Swift, there are several ways to check if an index exists in an array. You can use the startIndex and endIndex properties to compare the index against the range of valid indices, the contains() method of the indices property to check if the index is within that range, or the count property to check if the index is less than the length of the array.

You can also use a guard statement or a ternary operator to print a message saying whether the index exists or not or use the get method to retrieve the element at the index if it exists. The choice of method depends on the context and your personal preference.

Updated on: 04-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements