删除类型为 != something 的所有注释 (MyPointAnnotations)

Remove all annotations(MyPointAnnotations) where type != something

我想删除类型为 != "sport"

的所有注释 (MyPointAnnotations)
class MyPointAnnotation: MKPointAnnotation {
    var identifier: String?
    var time = Date()
    var creatorId: String?
    var createdAt: Date?
    var type: String?
}

我看到一个 Whosebug post 删除了所有 title == someString 的注释。但是我不知道如何实现我的目标。

计算器

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)

检查注解是否为MyPointAnnotation,类型是否为nil

let filteredAnnotations = view.annotations.filter { annotation in
    guard let type = (annotation as? MyPointAnnotation)?.type else { return false }
    return type != "sport"
}

考虑使用较少的选项。可选项不是不编写初始化程序的借口。如果每个注解都有一个标识符和一个类型,则将这两个属性都声明为非可选的。