首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKMapItem阵列中的双变量

MKMapItem阵列中的双变量
EN

Stack Overflow用户
提问于 2018-06-12 08:43:55
回答 1查看 113关注 0票数 0

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

bug图像> http://i67.tinypic.com/2jfyxdf.png MKMapItem示例

代码语言:javascript
复制
<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";
}

它的代码是:

代码语言:javascript
复制
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()
            }

        }
    }
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-12 08:57:53

更新后,样本按名称重复:

代码语言:javascript
复制
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()
    }
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50812875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档