How to find index of list item in Swift?

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

    Swift 2.1

    var array = ["0","1","2","3"]
    
    if let index = array.indexOf("1") {
       array.removeAtIndex(index)
    }
    
    print(array) // ["0","2","3"]
    

    Swift 3

    var array = ["0","1","2","3"]
    
    if let index = array.index(of: "1") {
        array.remove(at: index)
    }
    array.remove(at: 1)
    

提交回复
热议问题