快速展开价值

时间:2017-03-25 17:04:12

标签: swift xcode firebase-realtime-database unwrap gmsplace

所以我在解包和选择方面遇到了一些问题。我正在使用Google Places并将place.formattedAddress值传递两次...首先从GMSAutocompleteViewController传递到UIView以显示在字符串中。然后我想在用户确认该地点后将place.formattedAddress传递给Firebase。我能够将选定的GMSPlace传递给UIView,但是一旦我调用confirmAddPlace()函数,它就会在行>处崩溃。让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()
    }

1 个答案:

答案 0 :(得分:0)

不要在func中强行打开您的参数。要么将它们作为可选项,要么将它们用作类型。

制作可选:

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

按原样使用他们的类型:(但为此你必须使用unwraped值调用此func,例如:confirmAddPlace(myPlace!)

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

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

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

如果您的传递place

有疑问,这将从您的func返回