我一定有错误 - 将我的代码更改为以下但我仍然收到错误。我明显误解了一些事情。感谢任何帮助。
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
答案 0 :(得分:2)
MKLocalSearchCompletionHandler
的签名已更改,因此您需要像MKLocalSearch
这样调用启动方法。
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
循环,您只需要循环而不是for循环。
答案 1 :(得分:0)
发现我的错误:
建议的第1行:
中的search.start {(响应:MKLocalSearchResponse?,错误:错误?)然后是最后一行: } //作为? MKLocalSearchCompletionHandler
所以没有完成处理程序,它完美地运作