独特的MKA注释集

时间:2018-10-03 12:24:26

标签: swift mkannotation hashable

所以我想做的是更新mapview上的注释,但是我想先将它们放入Set中,以便检查现有的,新的和已删除的注释。因此,我正在检查Hashable protocol的符合性

  

协议“哈希”只能用作通用约束,因为   有自我或相关类型要求

关于如何做我想做的任何想法

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

1 个答案:

答案 0 :(得分:0)

所以我最终得到的是这个

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)