0

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()
    }
user3708224
  • 1,229
  • 4
  • 19
  • 37

1 Answers1

0

Don't force unwrap your parameters in func. Either you make them optional or you use them as their type.

For making optional:

func confirmAddPlace(place: GMSPlace?) {
    ...
    ...
    let placeID2 = place?.placeID
    ...
    ...
}

For using their type as is: (but for this you have to call this func with unwraped value, like: confirmAddPlace(myPlace!))

func confirmAddPlace(place: GMSPlace) {
    ...
    ...
    let placeID2 = place.placeID
    ...
    ...
}

But if you do want to stick with your implementation then you can just swap like this:

guard let place = place else {
    return
}
let placeID2 = place.placeID

This will return from your func if something is suspicious with your passed place

nayem
  • 7,285
  • 1
  • 33
  • 51
  • Thanks, but I made both GMSPlace and place option as mentioned in your first suggestion and it still crashes. I also tried the second option with unwrapping confirmAddPlace(selectedPlace!) and still no luck... – user3708224 Mar 26 '17 at 14:45
  • @user3708224 can you try the third one?? – nayem Mar 26 '17 at 15:43
  • yep - still crashing at my AppDelegate and giving me the terminating with uncaught exception of type NSException error....I am wondering if there is an easier way to extract the name value from the self.placeNameLabel.text = "Are you sure you would like to add \(name) to your places?" string saved in the func viewController()...?? – user3708224 Mar 26 '17 at 15:55