Is there a function in the Swift Standard library that acts on a collection, takes a predicate and returns the value removed from that collection?
Currently, I have to implement it in 2 steps:
guard let idx = allAnnotations.index(where: {$0 is MKUserLocation}) else {return}
let userLocation = allAnnotations.remove(at: idx) as! MKUserLocation
But I guess, a similar function exists.
The goal
I have the following array:
[Type1, Type1, Type1, Type1, Type1, Type1, Type2]
Type2
may or may not be present in the array. There are no other types except these two.
I need to split it onto two elements:
[Type1, Type1, Type1, Type1, Type1, Type1]
and
Type2?
That's the function I'm looking for.
Swift 5 has removeAll(where)
@inlinable public mutating func removeAll(where shouldBeRemoved: (Element) throws -> Bool) rethrows
You can use it like this -
var array = [1,2,3]
array.removeAll(where: { $0 == 2})
print(array) // [1,3]