gpt4 book ai didi

swift - 删除包含标题等于/不等于字符串的注释?

转载 作者:可可西里 更新时间:2023-11-01 00:58:02 25 4
gpt4 key购买 nike

我花了几天时间尝试删除标题等于或不等于从另一个 View Controller 的 uicollection View 单元格 didSelect 中选择的字符串的注释。我将字符串传递到包含我的 mapview 的 View Controller 。我使用自定义注释作为注释显示方式的模型。

如何按标题选择和删除自定义注释。我已经有一个字典数组,其中包含删除其他注释后注释将使用的数据。我知道如何删除所有注释,但不知道如何只删除标题等于/不等于搜索字符串的注释。

为什么网络上或当前的 swift 3 都没有这样的功能?

我想出了这个,但只删除了注释并显示了“filteredAnnotations”

 var filteredAnnotations = self.mapview.annotations.filter {($0.title != nil) && isEqual(searchString) }

print(filteredAnnotations)

self.mapview.removeAnnotations(self.mapview.annotations)
self.mapview.addAnnotations(filteredAnnotations)

使用打印语句只返回一个空数组“[]”

最佳答案

使用 filter 获取应删除的所有注释的列表(即其标题不是您的搜索字符串,但也不是 MKUserLocation)和然后删除它们。

在 Swift 3 中:

let filteredAnnotations = mapView.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 != searchString // remove those whose title does not match search string
}

mapView.removeAnnotations(filteredAnnotations)

显然,根据您的要求将 != 更改为 == 或其他任何内容,但这说明了使用 filter 的基本思想识别一组标题符合某些特定标准的注释。

对于 Swift 2,请参阅 previous revision of this answer .

关于swift - 删除包含标题等于/不等于字符串的注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40593539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com