Author Topic: How to Find an Object in an Array with a Specific Property Value  (Read 6591 times)


  • Administrator
  • *****
  • Posts: 719
    • View Profile
To search for an object in an array with a specific property value, you can use the array's filter method. For example, the following code returns an array of PersonEntity objects whose last name is "Smith":

Code: [Select]
let personEntity = self.personEntityList.filter{ $0.lastName == "Smith"}.first
When searching on a property that contains unique values (one that guarantees a single entity is found), you can use the first property to return a single entity:

Code: [Select]
let personEntity = self.personEntityList.filter{ $0.SSN == "123-45-6789"}.first
All the best!
Kevin McNeish
Author of Learn to Code in Swift:
Check out our Robot Tic-Tac-Toe app:
« Last Edit: November 11, 2015, 12:20:46 PM by kjmcneish »