Swift MKMapItem数组中的双变量

Swift MKMapItem数组中的双变量,swift,mkmapitem,Swift,Mkmapitem,我有一个搜索栏,它给出了它的名字,打印了在TableView中搜索的名字。在添加搜索的键之前,我正在检查我的数据库是否获得了该变量。如果我的数据库得到了它,我会在TableView中添加搜索到的单词。我的问题是,目前matchingItems或response.mapItems有两个或更多变量,并且在TableView中多次打印相同的名称。我已经花了很多时间来解决这个问题,但我不知道怎么解决 bug的图像> MKMapItem示例 <MKMapItem: 0x6000003566e0>

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

bug的图像> 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";
}
{
isCurrentLocation=0;
name=“Arco di Traiano”;
placemark=“Arco di Traiano,Via Traiano,82100 Benevento,Italia@+/-0.00m,区域CLCircularRegion(标识符:“半径1414.16',中心:,半径1414.16m)”;
时区=“欧洲/罗马(CEST)偏移7200(日光)”;
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()
            }

        }
    }
}
}
var matchingItems:[MKMapItem]=[]
扩展位置SearchTable:UISearchResultsUpdated{
func updateSearchResults(对于searchController:UISearchController){
如果searchController.searchBar.text==nil | |(searchController.searchBar.text?.count)!<1{
self.matchingItems.removeAll()
self.tableView.reloadData()
}
guard let mapView=mapView,
让searchBarText=searchController.searchBar.text else{return}
let request=MKLocalSearchRequest()
request.naturalLanguageQuery=searchBarText
request.region=mapView.region
let search=MKLocalSearch(请求:请求)
search.start{response,\ in
防护装置let响应=响应else{
返回
}
响应.mapItems.enumerated()中的(索引,名称){
if(checkIfDatabaseGotThis(key:String(name.name!))!=nil){
self.matchingItems.append(response.mapItems[index])
self.tableView.reloadData()
}
}
}
}
}

已更新,因此示例将按名称进行重复数据消除:

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()
    }
}
var seenNames=Set()
响应.mapItems.enumerated()中的(索引,名称){
let item=response.mapItems[index]
if(checkIfDatabaseGotThis(key:String(name.name!))!=nil&&!seenNames.contains(item.name)){
self.matchingItems.append(项目)
参见名称.插入(项目.名称)
self.tableView.reloadData()
}
}

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

您能用重复的值显示一个数据示例吗?还有,你认为什么是复制品?你想有唯一的名称、位置、ID吗?我想有唯一的名称,我放了一个bug的图像,等等,请发布实际数据,你正在使用,图像无助于找到解决方案。在您的控制台中查看
MKMapItems
的示例将非常有用。我已添加了itI,我已尝试使用这种方法,但它不会删除重复项:(我认为它不起作用,因为副本有一个不同的可哈希值,比如>第一个副本,而第二个我看到了,那么您认为什么是复制品?因为不同的哈希值将意味着该项没有相同的元数据。(<代码> 0x60800 034 CE40 < /代码>部分不是哈希,它是内存地址)。我的意思是,我不希望项目具有相同的名称,如果您看到我在主要帖子中的屏幕截图,您将了解我的意思。提前感谢您的时间,我已根据更新了重复数据消除的答案。如果您将seenNames声明为全局变量,则项目名称代码有效!谢谢!