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

kjmcneish

  • 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: https://itunes.apple.com/us/book/learn-to-code-in-swift/id942956811?mt=11
Check out our Robot Tic-Tac-Toe app: https://itunes.apple.com/us/app/robot-tic-tac-toe/id990638517?mt=8
« Last Edit: November 11, 2015, 12:20:46 PM by kjmcneish »