2

I have array of object and trying get the index for object which is having particular value

for ex. for simple array we can achieve this using

var numbers = [1, 2, 3, 4, 5]
let get3 = exchangeRateList.firstIndex(of: 3). // result 2

how to achieve same for array with objects (for ex. to get index of object having id 3 )

 var objarray = [Name]()
        objarray.append(Name(id: 1, name: "Nuibb"))
        objarray.append(Name(id: 2, name: "Smith"))
        objarray.append(Name(id: 3, name: "Pollock"))
        objarray.append(Name(id: 4, name: "James"))
        objarray.append(Name(id: 5, name: "Farni"))
        objarray.append(Name(id: 6, name: "Kuni"))
New iOS Dev
  • 1,937
  • 7
  • 33
  • 67

1 Answers1

7

Use firstIndex(where:) to get that working.

let index = objarray.firstIndex { $0.id == 3 } //2
PGDev
  • 23,751
  • 6
  • 34
  • 88