How to get the first index of an element in array with Swift

1 Answer

0 votes
let arr = ["swift", "c", "c++", "swift", "java"]

if let index = arr.firstIndex(of: "swift") {
    print(index)
}

if let index = arr.firstIndex(of: "c++") {
    print(index)
}

if let index = arr.firstIndex(of: "php") {
    print("index: \(index)")
}
else {
    print("not found")
}





/*
run:

0
2
not found

*/

 



answered Sep 3, 2020 by avibootz
...