MKMapItem数组中的双变量 [英] Double variable in MKMapItem array

查看:80
本文介绍了MKMapItem数组中的双变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SearchBar,它给它一个名称,它打印在TableView中搜索的名称.在添加搜索关键字之前,我正在检查数据库是否获取了变量.如果我的数据库知道了,我将搜索到的单词添加到TableView中.我的问题是,目前matchingItems或response.mapItems具有双变量或更多,并且在TableView中多次打印相同名称.我已经花了很多时间来解决这个问题,但是我不知道该怎么做.

I got a SearchBar that giving the name it print the name searched in a TableView. Before add the key searched I am checking if my Database got the variable. If my database got it I add the searched word in the TableView. My problem is that at the moment matchingItems or response.mapItems got double variable or more and it print a lot of times the same name in the TableView. I have tried a lot of time to fix that but I don't know how do it.

错误图片> http://i67.tinypic.com/2jfyxdf.png MKMapItem的示例

Image of bug > http://i67.tinypic.com/2jfyxdf.png Example of MKMapItem

<MKMapItem: 0x6000003566e0> {
isCurrentLocation = 0;
name = "Arco di Traiano";
placemark = "Arco di Traiano, Via Traiano, 82100 Benevento, Italia @ <+41.13253257,+14.77915406> +/- 0.00m, region CLCircularRegion (identifier:'<+41.13253316,+14.77915406> radius 1414.16', center:<+41.13253316,+14.77915406>, radius:1414.16m)";
timeZone = "Europe/Rome (CEST) offset 7200 (Daylight)";
url = "http://www.comune.benevento.it/bn2_pagine/_mediagallery/pid.php?id=11";
}

此代码:

var matchingItems: [MKMapItem] = []

extension LocationSearchTable : UISearchResultsUpdating {
    func updateSearchResults(for searchController: UISearchController) {

        if searchController.searchBar.text == nil || (searchController.searchBar.text?.count)! < 1 {
            self.matchingItems.removeAll()
            self.tableView.reloadData()
        }

        guard let mapView = mapView,
            let searchBarText = searchController.searchBar.text else { return }

        let request = MKLocalSearchRequest()
        request.naturalLanguageQuery = searchBarText
        request.region = mapView.region
        let search = MKLocalSearch(request: request)

        search.start { response, _ in
            guard let response = response else {
                return
            }
            for (index , name) in response.mapItems.enumerated() {

            if (checkIfDatabaseGotThis(key: String(name.name!)) != nil){
                self.matchingItems.append(response.mapItems[index])
                self.tableView.reloadData()
            }

        }
    }
}
}

推荐答案

已更新,因此该示例按名称进行了重复数据删除:

Updated so the sample deduplicates by name:

var seenNames = Set<String>()
for (index , name) in response.mapItems.enumerated() {
    let item = response.mapItems[index]
    if(checkIfDatabaseGotThis(key: String(name.name!)) != nil && !seenNames.contains(item.name)){
        self.matchingItems.append(item)
        seenNames.insert(item.name)
        self.tableView.reloadData()
    }
}

这应该根据名称从您的项目列表中删除所有重复项.它跟踪您所看到的所有现有名称.如果以前没有看到该名称,则将该项目添加到列表中.否则它将被忽略.

That should remove all duplicates from your list of items based on the name. It keeps track of all the existing names that you have seen. If the name hasn't been seen before, the item is added to the list. Otherwise it is ignored.

这篇关于MKMapItem数组中的双变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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