Reputation: 56
I am having trouble analyzing the results given by the callback of GMSPlacesClient findAutocompletePredictionsFromQuery:
, which is a GMSAutocompletePrediction array.
The app will crash on let searchResultsWayPoints = finalResults.map
,
stating
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: $0.placeID, address: $0.attributedFullText.string)
}
resolver.fulfill(searchResultsWayPoints)
}
}
}
Any help would be appreciated. Thank you.
Upvotes: 0
Views: 1746
Reputation: 56
So I have solved this problem, but I did not fix anything, since it is from the GooglePlaces framework.
In order to solve this, simply make results
as results:[Any]?
at the callback.
Then, at guard let
, safely convert it to [GMSAutocompletePrediction]
.
Here is the complete code
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: $0.placeID, address: $0.attributedFullText.string)
}
resolver.fulfill(searchResultsWayPoints)
}
}
}
Upvotes: 1