Remove Specific Array Element, Equal to String - Swift

前端 未结 8 1954
臣服心动
臣服心动 2020-12-23 16:07

Is there no easy way to remove a specific element from an array, if it is equal to a given string? The workarounds are to find the index of the element of the array you wish

8条回答
  •  南笙
    南笙 (楼主)
    2020-12-23 16:44

    It's not clear if by quicker you mean in terms of execution time or amount of code.

    In the latter case you can easily create a copy using the filter method. For example, given the following array:

    let array = ["1", "2", "3", "4", "5"]
    

    you can create a copy with all elements but "2" as:

    let filteredArray = array.filter { $0 != "2" }
    

提交回复
热议问题