I am having some issues storing place details from Google Places. I can build the app fine but then crashes when I select a place from the GMSAutocompleteViewController
...
"Could not cast value of type 'GMSPlace' (0x10075bec8) to 'NSString' (0x1afec1398)."
I would like to save the selected place data and then send that data to be displayed in a UIView and UITableView. Not sure what I am doing wrong...
// MARK: GOOGLE AUTO COMPLETE DELEGATE
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
// Do something with the selected place.
// A hotel in Saigon with an attribution.
// let placeID = "ChIJV4k8_9UodTERU5KXbkYpSYs"
let placeID = GMSPlace.self
placesClient.lookUpPlaceID((placeID as AnyObject) as! String, callback: { (place, error) -> Void in
if let error = error {
print("lookup place id query error: \(error.localizedDescription)")
return
}
guard let place = place else {
print("No place details for \(placeID)")
return
}
print("Place name \(place.name)")
print("Place address \(place.formattedAddress)")
print("Place placeID \(place.placeID)")
print("Place attributions \(place.attributions)")
})
self.dismiss(animated: true, completion: nil)
setupConfirmationPopUp()
}
Your code (GMSPlace.self
) is creating a reference to the GMSPlace
type itself, not a particular instance. The delegate method will provide back the instance of the GMSPlace
that the view controller was able to autocomplete to. Using this instance you can get the place ID from the property called placeID
.
Try.
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
// Do something with the selected place.
// A hotel in Saigon with an attribution.
// let placeID = "ChIJV4k8_9UodTERU5KXbkYpSYs"
let placeID = place.placeID
placesClient.lookUpPlaceID(placeID, callback: { (place, error) -> Void in
if let error = error {
print("lookup place id query error: \(error.localizedDescription)")
return
}
guard let place = place else {
print("No place details for \(placeID)")
return
}
print("Place name \(place.name)")
print("Place address \(place.formattedAddress)")
print("Place placeID \(place.placeID)")
print("Place attributions \(place.attributions)")
})
self.dismiss(animated: true, completion: nil)
setupConfirmationPopUp()
}