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

前端 未结 7 1816
走了就别回头了
走了就别回头了 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:17

    If you need to modify initial array, you can use the function removeAll(where:) that is available in Swift 4.2/Xcode 10:

    var arr = ["a", "b", "c", "b"]
    arr.removeAll(where: { $0 == "b" })
    print(arr) // output is ["a", "c"]
    

    However, if you are using Xcode 9 you can find this function in Xcode9to10Preparation (this library provides implementations of some new functions from Xcode 10).

提交回复
热议问题