How to find index of list item in Swift?

后端 未结 22 1571
离开以前
离开以前 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:39

    For SWIFT 3 you can use a simple function

    func find(objecToFind: String?) -> Int? {
       for i in 0...arrayName.count {
          if arrayName[i] == objectToFind {
             return i
          }
       }
    return nil
    }
    

    This will give the number position, so you can use like

    arrayName.remove(at: (find(objecToFind))!)
    

    Hope to be useful

提交回复
热议问题