Swift中的drop(where:{})remove(where:{})函数?

Ric*_*hiy 5 arrays collections swift

Swift标准库中是否有作用于集合的函数,采用谓词并返回从该集合中删除的值?

目前,我必须分两步实施:

guard let idx = allAnnotations.index(where: {$0 is MKUserLocation}) else {return}
let userLocation = allAnnotations.remove(at: idx) as! MKUserLocation
Run Code Online (Sandbox Code Playgroud)

但是我想,存在类似的功能。

目标

我有以下数组:

[Type1, Type1, Type1, Type1, Type1, Type1, Type2]
Run Code Online (Sandbox Code Playgroud)

Type2数组中可能存在也可能不存在。除了这两种以外,没有其他类型。

我需要将其分为两个元素:

[Type1, Type1, Type1, Type1, Type1, Type1]
Run Code Online (Sandbox Code Playgroud)

Type2?
Run Code Online (Sandbox Code Playgroud)

那就是我要寻找的功能。

sha*_*oga 5

斯威夫特 5 有removeAll(where)

@inlinable public mutating func removeAll(where shouldBeRemoved: (Element) throws -> Bool) rethrows
Run Code Online (Sandbox Code Playgroud)

你可以这样使用它 -

var array = [1,2,3]
array.removeAll(where: { $0 == 2})
print(array) // [1,3]
Run Code Online (Sandbox Code Playgroud)

苹果文档