0

所以我在展开和选项方面遇到了一些问题。我正在使用 Google Places 并将 place.formattedAddress 值传递两次...首先从 GMSAutocompleteViewController 到 UIView 以在字符串中显示。然后我想在用户确认地点后将 place.formattedAddress 传递给 Firebase。我可以将选定的 GMSPlace 传递到 UIView 中,但是一旦我调用了 confirmAddPlace() 函数,它就会在 line > let placeID2 = place.placeID 处崩溃,并给我“以 NSException 类型的未捕获异常终止”。我相信它与展开和选项有关......我对这个概念仍然有些陌生。感谢您的任何帮助!

// 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()
    }
4

1 回答 1

0

不要在 func 中强制解包参数。要么将它们设为可选,要么将它们用作它们的类型。

为了使可选:

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

对于按原样使用它们的类型:(但是为此,您必须使用未包装的值调用此函数,例如confirmAddPlace(myPlace!):)

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

但是如果你确实想坚持你的实现,那么你可以像这样交换:

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

如果您的传递有可疑之处,这将从您的函数返回place

于 2017-03-25T19:13:18.320 回答