-2

Suppose I am searching for cardType = points, if its found I want to know the index position of that object. According to the given example array, index position of this object is 1. Below is the example array.

[{
cardCode = MGC;
cardId = 5720663d8944bf6710c9d894;
cardType = gift;
},
{
cardCode = MPC;
cardId = 5720663d8944bf6710c9d898;
cardType = points;
}, 
{
cardCode = MAC;
cardId = 5720663d8944bf6710c9d895;
cardType = advantage;
},
{
cardCode = MCC;
cardId = 5720663d8944bf6710c9d897;
cardType = cashback;
}]

Error

Kunal Kumar
  • 1,722
  • 1
  • 17
  • 32
  • Both question are different. I not accecing the array which contain element of type String. The answer based on the method `index(of)` which is not available in my case. – Kunal Kumar Mar 09 '17 at 12:48
  • Did you read *all* answers of that Q&A? http://stackoverflow.com/a/24069331/1187415 and http://stackoverflow.com/a/32923407/1187415 both explain how to use `index(where: ..)` – Martin R Mar 09 '17 at 13:30

1 Answers1

4

You can use index(where:) for that.

if let index = yourArray.index(where: { $0.cardType == "points" }) {
    print(index) //1
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183