我想删除所有类型为!=“ sport”的注释(MyPointAnnotations)
class MyPointAnnotation: MKPointAnnotation {
var identifier: String?
var time = Date()
var creatorId: String?
var createdAt: Date?
var type: String?
}
我看到了一个stackoverflow帖子,其中删除了title == someString的所有注释。但是我不知道如何实现自己的目标。
Stackoverflow
let filteredAnnotations = view.annotations.filter { annotation in
if annotation is MKUserLocation { return false }// don't remove MKUserLocation
guard let title = annotation.title else { return false }// don't remove annotations without any title
return title != "sport"// remove those whose title does not match search string
}
view.removeAnnotations(filteredAnnotations)
答案 0 :(得分:0)
检查注释是否为MyPointAnnotation
并且类型不是nil
let filteredAnnotations = view.annotations.filter { annotation in
guard let type = (annotation as? MyPointAnnotation)?.type else { return false }
return type != "sport"
}
考虑使用较少的可选内容。可选参数不是编写初始化程序的理由。如果每个注释都有一个标识符和一个类型,则将两个属性都声明为非可选。