回顾一下Swift中的repo项目之后,我意识到有一个代码问题,我很难找出与 MKLocalSearch的特定函数的API更改相关的错误
具体来说,在我的下面代码片段中主要包含函数,基本上搜索并更新到搜索到的位置
func searchBarSearchButtonClicked (_ searchBar: UISearchBar){
//here check if local search existed, it then cancel the search
if let search = localSearch {
search.cancel()
}
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchBar.text
localSearch = MKLocalSearch(request: request)
//set the network activity visible
UIApplication.shared
.isNetworkActivityIndicatorVisible = true
//start the search and deliver the response with error handling asynchronously
/* --------------------------This is where the bug occurs --------------*/
localSearch!.start {
[weak self] (response : MKLocalSearchResponse?, error: NSError?) in
if error != nil {
return
}
UIApplication.shared.isNetworkActivityIndicatorVisible = false
//safely extract location search results based on response
guard let myResponse = response else {
print("error in search: \(error?.localizedDescription ?? MissingError)")
return
}
// guard let location = myResponse.mapItems.first else {
// print("There are no results")
// return
// }
guard let location = myResponse.mapItems else{
print("There are no results")
return
}
self?.updateMapWithItem(location, region: myResponse.boundingRegion)
}
所以我收到错误的完整信息
Cannot convert value of type '(MKLocalSearchResponse?, NSError?) -> ()' to expected argument type 'MKLocalSearchCompletionHandler' (aka '(Optional<MKLocalSearchResponse>, Optional<Error>) -> ()')
Swift 3.0编译器建议我执行以下操作,但它的解决方案完全足够,因为它只是一直告诉我在不完全解决问题的情况下插入越来越多的以下语句。
Insert ' as! MKLocalSearchCompletionHandler'
我实际上想将整个项目代码从Swift 2.x迁移到4.0,但我的意思是我需要首先从2.x迁移到3.0。
答案 0 :(得分:0)
使用
localSearch?.start(completionHandler: {
[weak self] (response, error) in
....
})
而不是
localSearch!.start {
[weak self] (response : MKLocalSearchResponse?, error: NSError?) in
....
}