Unique set of MKAnnotations

62 Views Asked by At

So what I'm trying to do is to update the annotations on a mapview, but I want to put them into a Set first so to check for existing, new and removed annotations. I'm having some checking for conformance of the Hashable protocol because of this

Protocol 'Hashable' can only be used as a generic constraint because it has Self or associated type requirements

Any ideas on how to do what I want

var set: Set<Hashable> = Set()
for annotation in self.annotations {
     guard let hashable = annotation as? Hashable else { continue }
     set.update(with: hashable)
}
1

There are 1 best solutions below

0
On

So what i ended up with is this

let newHashValues = Set(new.map{ $0.hash })
let existingHashValues = Set(self.annotations.map{ $0.hash })

let removedHashes = existingHashValues.subtracting(newHashValues)
let addedHashed = newHashValues.subtracting(existingHashValues)

let removed = self.base.annotations.filter({ removedHashes.contains($0.hash) })
let added = element.filter({ addedHashed.contains($0.hash) })

self.removeAnnotations(removed)
self.addAnnotations(added)