Remove Specific Array Element, Equal to String - Swift

前端 未结 8 1942
臣服心动
臣服心动 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:41

    var ra = ["a", "ab", "abc", "a", "ab"]
    
    print(ra)                               // [["a", "ab", "abc", "a", "ab"]
    
    ra.removeAll(where: { $0 == "a" })
    
    print(ra)                               // ["ab", "abc", "ab"]
    

提交回复
热议问题