Author Topic: How to find a specific object in an array in Swift  (Read 5068 times)

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
How to find a specific object in an array in Swift
« on: October 14, 2015, 10:52:44 AM »
To find a specific object in an array in Swift, you pass a predicate that uses Swift's identity operator. For example:

Code: [Select]
let memberEntity1 = member.createEntity()
let memberEntity2 = member.createEntity()
let members: Array<MemberEntity> = [memberEntity1, memberEntity2];
let index = members.indexOf({$0 === memberEntity2})

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:16:21 PM by kjmcneish »