I need to search an array of objects for a specific value.
Is there a functionality in swift equivalent to for(Distance d : distances) in Java.
2 Answers
You can use something like below
let arr = [1,2,3,4,5]
let index = arr.firstIndex(of: 3)
the index will give the first index of the matching object. There are other variations to it. Check here for more details https://developer.apple.com/documentation/swift/array/1848165-first
Update: Specific to your query
struct Test {
let number: Int
}
let arr = [Test(number: 1),Test(number: 2),Test(number: 3),Test(number: 4),Test(number: 5)]
let index = arr.firstIndex(where: {$0.number == 4 })
the index will give the first index of the matching object.