find the index item in an array

Hello,

I want to check if the element 'Medic()' exists in my array.

`var array = [Medic(), AssaultSoldier(), Sniper()] `

Thank you all.
Answered by DTS Engineer in 661074022
Swift’s standard library includes a wealth of algorithms that you can reuse and thus make your code simpler and smaller. For example:

Code Block
class Soldier { }
class Medic: Soldier { }
class AssaultSoldier: Soldier { }
class Sniper: Soldier { }
let array = [Medic(), AssaultSoldier(), Sniper()]
let containsMedic = array.contains(where: { $0 is Medic })
print(containsMedic) // true
let indexOfFirstMedic = array.firstIndex(where: { $0 is Medic })
print(indexOfFirstMedic) // Optional(0)


The contains(where:) method returns true if the supplied closure return true for any item in the collection, while the firstIndex(where:) returns the index of that item.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"

I want to check if the element 'Medic()' exists in my array. 

It depends on the definition of Medic and other types in the array.
These are Class !

These are Class !

Stating it as class is not the definition.
I did the following small demo to show:

Code Block
class Player {
var name: String = "Player"
}
class Medic : Player {
override init() {
super.init()
name = "Medic"
}
}
class AssaultSoldier : Player {
override init() {
super.init()
name = "AssaultSoldier"
}
}
class Sniper : Player {
override init() {
super.init()
name = "Sniper"
}
}

then

Code Block
    var array = [Medic(), AssaultSoldier(), Sniper()]
   let thereIsMedic = array.filter {$0 is Medic }.count > 0
print(thereIsMedic)


And to get the position of Medic:
Code Block
for (pos, item) in array.enumerated() {
if item is Medic {
print("position of Medic is", pos)
break
}
}


If that's what you're looking for, don't forget to close the thread
You could also use map:

Code Block
let boolArray = array.map {$0 is Medic}
let index = boolArray.firstIndex(of: true) ?? -1
print("Medic at", index)

Or, in a more condensed form (here to find Sniper)
Code Block
let sniperIndex = array.map {$0 is Sniper}.firstIndex(of: true) ?? -1
print("Sniper at", sniperIndex)

Accepted Answer
Swift’s standard library includes a wealth of algorithms that you can reuse and thus make your code simpler and smaller. For example:

Code Block
class Soldier { }
class Medic: Soldier { }
class AssaultSoldier: Soldier { }
class Sniper: Soldier { }
let array = [Medic(), AssaultSoldier(), Sniper()]
let containsMedic = array.contains(where: { $0 is Medic })
print(containsMedic) // true
let indexOfFirstMedic = array.firstIndex(where: { $0 is Medic })
print(indexOfFirstMedic) // Optional(0)


The contains(where:) method returns true if the supplied closure return true for any item in the collection, while the firstIndex(where:) returns the index of that item.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
find the index item in an array
 
 
Q