删除包含等于/不等于字符串的标题的注释? [英] Remove annotations containing a title equal/not equal to a String?

查看:32
本文介绍了删除包含等于/不等于字符串的标题的注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了几天时间试图删除标题等于或不等于从另一个视图控制器的 uicollection 视图单元 didSelect 中选择的字符串的注释.我将字符串传递给包含我的地图视图的视图控制器.我使用自定义注释作为注释显示方式的模型.

I have looked for a couple of days on trying to remove annotations that a title equal or not equal to a string that is selected from a uicollection View cell didSelect from another view controller. I get the string passed to my view controller that contains my mapview. I use a custom annotation that is the model for how the annotations are displayed.

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

How do I select and remove the custom annotations by their title. I already have an array of dictionaries that contains the data the annotations will use once the other annotations are removed. I know how to remove ALL the annotations, but not how to just remove the ones that have titles that are equal/not equal to the search string.

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

Why is nothing on the web or current for swift 3 for such a function?

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

I have come up with this, but only removed annotations and does display the "filteredAnnotations"

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

 print(filteredAnnotations)

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

使用print语句只返回一个空数组[]"

using the print statement only returns an empty array of "[]"

推荐答案

使用 filter 获取所有应删除的注释的列表(即其标题不是您的搜索字符串,而是't 一个 MKUserLocation,或者)然后删除它们.

Use filter to get a list of all annotations that should be removed (i.e. whose title is not your search string, but isn't a MKUserLocation, either) and then remove them.

在 Swift 3 中:

In 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 的基本思想识别标题与某些特定条件匹配的一组注释.

Obviously, change that != to == as suits your requirements, or whatever, but this illustrates the basic idea of using filter to identify a set of annotations whose title matches some particular criteria.

对于 Swift 2,请参阅此答案的先前修订版.

For Swift 2, see previous revision of this answer.

这篇关于删除包含等于/不等于字符串的标题的注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆