I have a map where after I added a component from an array I want that specific component to be deleted from that array. I tried methods of filtering then removing but it only removes one item from the array. I need each item of the array deleted after its been used. This is what it looks like:
private var labelViews: [MapLabelView] = []
private func removeAllLabels() {
guard let mapController = viewModel.mapController,
let currentMap = mapController.currentMap else {
return
}
> labelViews.forEach { view in
> DispatchQueue.main.async {
> mapController.removeComponent(view, on: currentMap)
> self.labelViews.removeAll(where: {$0 == view})
if let index = self.campusLabelViews.firstIndex(of: view)
{self.campusLabelViews.remove(at: index)}//This is what I tried doing, but its only removing the first view and not ones after that.
}
here is the extension through which you can get all indexes of certain view present in array
extension Array where Element: Equatable {
func allIndexes(of element: Element) -> [Int] {
return self.enumerated().filter({ element == $0.element }).map({ $0.offset })
}
}
so your code will become
let indexs = self.campusLabelViews. allIndexes(of: view) // it return all indexes of that particular view
for index in indexs {
self.campusLabelViews.remove(at: index)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.