所以我在展开和选项方面遇到了一些问题。我正在使用 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()
}