How to remove an element of a given value from an array in Swift

前端 未结 7 1817
走了就别回头了
走了就别回头了 2020-12-28 11:34

I want to remove all elements of value x from an array that contains x, y and z elements

let arr = [\'a\', \'b\', \'c\', \'b\']

How can I r

7条回答
  •  星月不相逢
    2020-12-28 12:29

    var array : [String]
    array = ["one","two","one"]
    
    let itemToRemove = "one"
    
    while array.contains(itemToRemove) {
        if let itemToRemoveIndex = array.index(of: itemToRemove) {
            array.remove(at: itemToRemoveIndex)
        }
    }
    
    print(array)
    

    Works on Swift 3.0.

提交回复
热议问题