输入多个单词时,Swift UIText字段抛出零错误

输入多个单词时,Swift UIText字段抛出零错误,swift,uitextfield,mapkit,optional,Swift,Uitextfield,Mapkit,Optional,我的程序使用的是map,我有一个UITextField,用户在其中输入搜索短语,然后map会在map上显示搜索结果的注释。 该程序在模拟器中运行良好,但在我的手机上测试时,如果我输入两个工作,例如“dog park”,它会抛出一个“nil,同时打开可选值”错误。但是,如果我只输入“dog”,程序不会在我的手机上抛出错误。我已在ViewDidLoad中设置了代理,并且我的所有出口都已正确链接。 我在这里搜索了一下,试图解决这个问题,但没有结果,所以我想问问你们,看看你们是否注意到我遗漏了什么 以下

我的程序使用的是map,我有一个UITextField,用户在其中输入搜索短语,然后map会在map上显示搜索结果的注释。 该程序在模拟器中运行良好,但在我的手机上测试时,如果我输入两个工作,例如“dog park”,它会抛出一个“nil,同时打开可选值”错误。但是,如果我只输入“dog”,程序不会在我的手机上抛出错误。我已在ViewDidLoad中设置了代理,并且我的所有出口都已正确链接。 我在这里搜索了一下,试图解决这个问题,但没有结果,所以我想问问你们,看看你们是否注意到我遗漏了什么

以下是UITextLabel搜索的函数:

@IBAction func searchField(_ sender: UITextField) {
    let allAnnotations = self.mapView.annotations
    self.mapView.removeAnnotations(allAnnotations)
    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = textField.text!
    request.region = mapView.region

    let search = MKLocalSearch(request: request)
    search.start(completionHandler: {(response, error) in

        if error != nil{
            print("Error occured in search: \(error!.localizedDescription)")
        }else if response!.mapItems.count == 0{
            self.textField.text = "No matches found"
        }else{
            print("Matches found")
            for item in response!.mapItems{
                let annotation = MKPointAnnotation()
                annotation.coordinate = item.placemark.coordinate
                annotation.title = item.name
                annotation.subtitle = item.placemark.thoroughfare! + ", " + item.placemark.locality! + ", " + item.placemark.administrativeArea!
                self.mapView.addAnnotation(annotation)
            }
        }
    })
    sender.resignFirstResponder()
}

因此,在进一步研究之后,我发现了一个答案,它不需要添加到naturalLanguageQuery字符串中。问题是for循环返回的是空值。让一个守卫进来作为回应解决了这个问题

@IBAction func searchField(_ sender: UITextField) {
    let allAnnotations = self.mapView.annotations
    self.mapView.removeAnnotations(allAnnotations)
    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = textField.text!
    request.region = mapView.region

    let search = MKLocalSearch(request: request)
    search.start(completionHandler: {(response, error) in

        if error != nil{
            print("Error occured in search: \(error!.localizedDescription)")
        }else if response!.mapItems.count == 0{
            self.textField.text = "No matches found"
        }else{
            print("Matches found")
            guard let response = response?.mapItems else { return }
            for item in response{
                let annotation = MKPointAnnotation()
                annotation.coordinate = item.placemark.coordinate
                annotation.title = item.name
                annotation.subtitle = item.placemark.thoroughfare! + ", " + item.placemark.locality! + ", " + item.placemark.administrativeArea!

                self.mapView.addAnnotation(annotation)
            }
        }
    })
    sender.resignFirstResponder()
}

因此,在进一步研究之后,我发现了一个答案,它不需要添加到naturalLanguageQuery字符串中。问题是for循环返回的是空值。让一个守卫进来作为回应解决了这个问题

@IBAction func searchField(_ sender: UITextField) {
    let allAnnotations = self.mapView.annotations
    self.mapView.removeAnnotations(allAnnotations)
    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = textField.text!
    request.region = mapView.region

    let search = MKLocalSearch(request: request)
    search.start(completionHandler: {(response, error) in

        if error != nil{
            print("Error occured in search: \(error!.localizedDescription)")
        }else if response!.mapItems.count == 0{
            self.textField.text = "No matches found"
        }else{
            print("Matches found")
            guard let response = response?.mapItems else { return }
            for item in response{
                let annotation = MKPointAnnotation()
                annotation.coordinate = item.placemark.coordinate
                annotation.title = item.name
                annotation.subtitle = item.placemark.thoroughfare! + ", " + item.placemark.locality! + ", " + item.placemark.administrativeArea!

                self.mapView.addAnnotation(annotation)
            }
        }
    })
    sender.resignFirstResponder()
}

错误发生在哪一行?您应该考虑使用<代码>防护程序> <代码> >代码> >如果允许< <代码>,而不是强制使用<代码>展开!<代码>。这也可以帮助你理解哪里出了问题。试过之后,它仍然抛出了错误。在调试器中逐步执行代码,并在“self.mapView.addAnnotation”行后抛出错误。真正奇怪的是,如果你输入一个单词就可以了,但输入两个单词就会出错。更不用说它在模拟器中工作得很完美。在哪一行你会出错?我在上面的评论中提到了这一行。解决了它。我在naturalLanguageQuery中添加了“loc:nearly”。现在是request.naturalLanguageQuery=textField.text!+“+”loc:nearly“在哪一行发生错误?您应该考虑使用<代码>防护程序> <代码> >代码> >如果允许< <代码>,而不是强制使用<代码>展开!<代码>。这也可以帮助你理解哪里出了问题。试过之后,它仍然抛出了错误。在调试器中逐步执行代码,并在“self.mapView.addAnnotation”行后抛出错误。真正奇怪的是,如果你输入一个单词就可以了,但输入两个单词就会出错。更不用说它在模拟器中工作得很完美。在哪一行你会出错?我在上面的评论中提到了这一行。解决了它。我在naturalLanguageQuery中添加了“loc:nearly”。现在是request.naturalLanguageQuery=textField.text!+“+”位置:附近”