如何解决iOS MKLocalSearch导致崩溃
我正在使用UISearchBar和UITableView执行MKLocalSearch。
第一个搜索始终效果很好,但是如果您尝试另一个搜索,则应用程序崩溃,并且出现“索引超出范围错误”。让我知道是否需要更多信息,谢谢。
ref.on("child_added",function(snapshot,peviousChildKey) {
// Add to the DOM
})
ref.on("child_changed",peviousChildKey) {
// Update in the DOM
})
ref.on("child_removed",function(snapshot) {
// Remove from the DOM
})
ref.on("child_moved",peviousChildKey) {
// Move in the DOM
})
在尝试第三或第四次搜索后,表中的结果保持不变,并且不再记录任何结果。我收到错误消息:
错误:该操作无法完成。 (MKErrorDomain错误3。)
解决方法
您的问题是您过早更改了searchResults。试试:
func searchBar(_ searchBar: UISearchBar,textDidChange searchText: String) {
print("searchText \(searchText)")
let searchRequest = MKLocalSearch.Request()
searchRequest.naturalLanguageQuery = searchText
let search = MKLocalSearch(request: searchRequest)
search.start { response,error in
guard let response = response else {
print("Error: \(error?.localizedDescription ?? "Unknown error").")
return
}
var newArray: [YourStruct] = []
for item in response.mapItems {
newArray.append(item.placemark)
}
self.searchResults = newArray
self.tableView.reloadData()
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。