How to find index of list item in Swift?

后端 未结 22 1819
离开以前
离开以前 2020-11-22 06:17

I am trying to find an item index by searching a list. Does anybody know how to do that?

I see there is list.StartIndex and <

22条回答
  •  迷失自我
    2020-11-22 06:30

    Any of this solution works for me

    This the solution i have for Swift 4 :

    let monday = Day(name: "M")
    let tuesday = Day(name: "T")
    let friday = Day(name: "F")
    
    let days = [monday, tuesday, friday]
    
    let index = days.index(where: { 
                //important to test with === to be sure it's the same object reference
                $0 === tuesday
            })
    

提交回复
热议问题