So I am having some issues with unwrapping and optionals. I am using Google Places and passing the place.formattedAddress value twice...first from the GMSAutocompleteViewController to a UIView to be displayed within a string. Then I want to pass the place.formattedAddress to Firebase after the user confirms the place. I am able to pass the selected GMSPlace into the UIView, but then once I call the confirmAddPlace() function it crashes at line > let placeID2 = place.placeID and gives me the "terminating with uncaught exception of type NSException". I believe it has something to do with unwrapping and optionals...I am still somewhat new to this concept. Thanks for any and all help!
// Pass GMSPlace to UIView string
// MARK: GOOGLE AUTO COMPLETE DELEGATE
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
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)")
})
let selectedPlace = place.formattedAddress
if let name = selectedPlace as String!
{
self.placeNameLabel.text = "Are you sure you would like to add \(name) to your places?"
}
self.dismiss(animated: true, completion: nil)
setupConfirmationPopUp()
}
// user taps confirm button to send item to be updated in Firebase
func confirmAddPlace(place: GMSPlace!) {
let accessToken = FBSDKAccessToken.current()
guard let accessTokenString = accessToken?.tokenString else { return }
let credentials = FIRFacebookAuthProvider.credential(withAccessToken: accessTokenString)
FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in
if error != nil {
print("Something went wrong with our FB user: ", error ?? "")
return
}
// create place on tap pf confirm
let placeID2 = place.placeID
guard let place = place else {
print("No place details for \(placeID2)")
return
}
let confirmedPlace = place.formattedAddress
if let confirmedPlace = confirmedPlace as String!
{
self.placeNameLabel.text = "\(confirmedPlace)"
}
let ref = FIRDatabase.database().reference().child("places")
let childRef = ref.childByAutoId()
let values = ["place": self.placeNameLabel.text, "name": user!.displayName]
childRef.updateChildValues(values)
})
animateOut()
}