如何解决 NSArray 元素无法匹配 GooglePlaces API findAutocompletePredictions 中的 Swift 数组元素类型?
How to Solve NSArray element failed to match the Swift Array Element type in GooglePlaces API findAutocompletePredictions?
我无法分析 GMSPlacesClient findAutocompletePredictionsFromQuery:
的回调给出的结果,这是一个 GMSAutocompletePrediction 数组。
应用程序将在 let searchResultsWayPoints = finalResults.map
崩溃,
说明
Precondition failed: NSArray element failed to match the Swift Array Element type
Expected GMSAutocompletePrediction but found GMSAutocompletePrediction
func getGoogleWayPoint(text: String) -> Promise<[GoogleWayPoint]> {
return Promise<[GoogleWayPoint]> { resolver in
if text.isEmpty {
resolver.fulfill([])
return
}
placesClient.findAutocompletePredictions(fromQuery: text, filter: filter, sessionToken: sessionToken) { (results, error) in
if let error = error {
dprint(error.localizedDescription)
resolver.fulfill([])
return
}
guard let finalResults = results else {
dprint("can't found results")
resolver.fulfill([])
return
}
let searchResultsWayPoints = finalResults.map {
GoogleWayPoint(id: [=13=].placeID, address: [=13=].attributedFullText.string)
}
resolver.fulfill(searchResultsWayPoints)
}
}
}
如有任何帮助,我们将不胜感激。谢谢。
所以我已经解决了这个问题,但我没有解决任何问题,因为它来自 GooglePlaces 框架。
为了解决这个问题,只需在回调中将 results
设为 results:[Any]?
。
然后,在 guard let
,安全地将其转换为 [GMSAutocompletePrediction]
。
这里是完整的代码
func getGoogleWayPoint(text: String) -> Promise<[GoogleWayPoint]> {
return Promise<[GoogleWayPoint]> { resolver in
if text.isEmpty {
resolver.fulfill([])
return
}
placesClient.findAutocompletePredictions(fromQuery: text, filter: filter, sessionToken: sessionToken) { (results: [Any]?, error) in
if let error = error {
dprint(error.localizedDescription)
resolver.fulfill([])
return
}
guard let finalResults = results as? [GMSAutocompletePrediction] else {
dprint("can't found results")
resolver.fulfill([])
return
}
let searchResultsWayPoints = finalResults.map {
GoogleWayPoint(id: [=10=].placeID, address: [=10=].attributedFullText.string)
}
resolver.fulfill(searchResultsWayPoints)
}
}
}
我无法分析 GMSPlacesClient findAutocompletePredictionsFromQuery:
的回调给出的结果,这是一个 GMSAutocompletePrediction 数组。
应用程序将在 let searchResultsWayPoints = finalResults.map
崩溃,
说明
Precondition failed: NSArray element failed to match the Swift Array Element type
Expected GMSAutocompletePrediction but found GMSAutocompletePrediction
func getGoogleWayPoint(text: String) -> Promise<[GoogleWayPoint]> {
return Promise<[GoogleWayPoint]> { resolver in
if text.isEmpty {
resolver.fulfill([])
return
}
placesClient.findAutocompletePredictions(fromQuery: text, filter: filter, sessionToken: sessionToken) { (results, error) in
if let error = error {
dprint(error.localizedDescription)
resolver.fulfill([])
return
}
guard let finalResults = results else {
dprint("can't found results")
resolver.fulfill([])
return
}
let searchResultsWayPoints = finalResults.map {
GoogleWayPoint(id: [=13=].placeID, address: [=13=].attributedFullText.string)
}
resolver.fulfill(searchResultsWayPoints)
}
}
}
如有任何帮助,我们将不胜感激。谢谢。
所以我已经解决了这个问题,但我没有解决任何问题,因为它来自 GooglePlaces 框架。
为了解决这个问题,只需在回调中将 results
设为 results:[Any]?
。
然后,在 guard let
,安全地将其转换为 [GMSAutocompletePrediction]
。
这里是完整的代码
func getGoogleWayPoint(text: String) -> Promise<[GoogleWayPoint]> {
return Promise<[GoogleWayPoint]> { resolver in
if text.isEmpty {
resolver.fulfill([])
return
}
placesClient.findAutocompletePredictions(fromQuery: text, filter: filter, sessionToken: sessionToken) { (results: [Any]?, error) in
if let error = error {
dprint(error.localizedDescription)
resolver.fulfill([])
return
}
guard let finalResults = results as? [GMSAutocompletePrediction] else {
dprint("can't found results")
resolver.fulfill([])
return
}
let searchResultsWayPoints = finalResults.map {
GoogleWayPoint(id: [=10=].placeID, address: [=10=].attributedFullText.string)
}
resolver.fulfill(searchResultsWayPoints)
}
}
}