我一定是搞错了-把我的代码改成了下面的代码,但是我还是有一个错误。我显然误解了什么。感谢你的帮助。

 func performSearch()  {
    mapView.delegate = self
    matchingItems.removeAll()
    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = searchText.text
    request.region = mapView.region
    let search = MKLocalSearch(request: request)

    search.start(completionHandler: ({(response: MKLocalSearchResponse?,error: NSError?) in
        if error != nil {
            return
        }
        if response!.mapItems.count == 0 {
            self.resultOfSearch.text = ("No matches found")

        }else{
            self.found = response!.mapItems
            if let items = response?.mapItems, items.count > 0 {
                while self.n < 3 && self.n < response!.mapItems.count { self.locationsRet[(response!.mapItems[self.n].name!)] = self.n; self.n = self.n + 1}
                self.n = 0
            }else{
                self.alert2("NOLOC")
            }

            self.alert3(self.locationsRet, found: self.found)
        }

        } as? MKLocalSearchCompletionHandler)!)
}

但还是会犯错误-我做错了什么?
此代码在Swift 2中运行良好,但在Swift 3中崩溃。
func performSearch()  {
    mapView.delegate = self
    matchingItems.removeAll()
    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = searchText.text
    request.region = mapView.region
    let search = MKLocalSearch(request: request)
    search.start(completionHandler: ({(response: MKLocalSearchResponse?,error: NSError?) in
        if error != nil {
            //self.resultOfSearch.text = ("Error occured in search:\(error!.localizedDescription)")
        } else if response!.mapItems.count == 0 {
            self.resultOfSearch.text = ("No matches found")
            //  }else if response!.mapItems.count > 1 {
        }else{
            self.found = response!.mapItems
            for _ in response!.mapItems {
                if response!.mapItems.count > 0 {
                    while self.n < 3 && self.n < response!.mapItems.count { self.locationsRet[(response!.mapItems[self.n].name!)] = self.n; self.n = self.n + 1}
                    self.n = 0
                }else{
                    self.alert2("NOLOC")

                }
            }
            self.alert3(self.locationsRet, found: self.found)
        }

        } as? MKLocalSearchCompletionHandler)!)
}

这段代码现在给出了这个错误
;
 function signature specialization <preserving fragile attribute, Arg[1] = [Closure Propagated : reabstraction thunk helper from  @callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> () to @callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> (@out ()), Argument Types : [@callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> ()]> of generic specialization <preserving fragile attribute, ()> of Swift.StaticString.withUTF8Buffer <A> ((Swift.UnsafeBufferPointer<Swift.UInt8>) -> A) -> A
->  0x5bf2e0 <+124>: trap

最佳答案

MKLocalSearchCompletionHandler的签名已更改,因此您需要这样调用MKLocalSearch上的start方法。

search.start { (response: MKLocalSearchResponse?, error: Error?) in
    if error != nil {
        return
    }
    if let items = response?.mapItems, items.count > 0 {
        //access the items using while loop only
    }
    else {
         self.alert2("NOLOC")
    }
}

注意:在您的代码中没有必要使用for循环,您只需要while循环而不是for one循环。

关于ios - Swift3中用于位置搜索的完成处理程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41846708/

10-12 00:22