Swift 文档ID检索
这应该从结果中获取用户ID!。uid并将其存储为变量或函数,以便我再次使用它。 问题是,我不知道如何让它存储这个函数之外的值。 我试图将它存储到初始按钮函数之外的变量中,我还试图通过删除使其变为空的部分代码将它返回到函数之外。我不确定我需要去哪里/我还可以尝试做些什么来解决这个问题。 如果有人知道如何从这个代码中检索我的文档ID,您的帮助将不胜感激Swift 文档ID检索,swift,firebase,google-cloud-firestore,Swift,Firebase,Google Cloud Firestore,这应该从结果中获取用户ID!。uid并将其存储为变量或函数,以便我再次使用它。 问题是,我不知道如何让它存储这个函数之外的值。 我试图将它存储到初始按钮函数之外的变量中,我还试图通过删除使其变为空的部分代码将它返回到函数之外。我不确定我需要去哪里/我还可以尝试做些什么来解决这个问题。 如果有人知道如何从这个代码中检索我的文档ID,您的帮助将不胜感激 @IBAction func NextButtonTapped(_ sender: Any) { //validate the filed
@IBAction func NextButtonTapped(_ sender: Any) {
//validate the fileds
let Error = validateFields()
if Error != nil {
// there is somthing wrong with the fields show error message
showError(Error!)
}
else {
// create cleaned versions of the data
let Password = PasswordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let Email = EmailTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let Firstname = FirstnameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let Lastname = LastnameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let Age = AgeTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
// create the user
Auth.auth().createUser(withEmail: Email, password: Password) { (results, Err) in
// check for errors
if Err != nil {
// there was an error creating the user
self.showError("Error creating user")
}
else {
// user was created succesfully store first and last name
let db = Firestore.firestore()
db.collection("users").document(results!.user.uid).setData(["first name":Firstname, "last name":Lastname, "age":Age, "uid":results!.user.uid]) { (Error) in
if Error != nil {
// show error message
self.showError("error saving user data")
}
//showing users document id
}
//transition to the home screen
self.transitionToHome()
}
}
}
}
我不知道该怎么做任何帮助都会很神奇
非常感谢 在iAction函数之外定义uid变量,如下所示
var uid: String? = nil
然后,在createUser函数中
self.uid = results!.user.uid
谢谢Sanzio,如果我想从一个不同的视图控制器访问这个uid,我该怎么做才能让另一个视图控制器不说“使用未解析的标识符”。有很多方法可以做到这一点,这取决于您如何设置视图控制器堆栈。你能提供更多的背景吗?例如,这是否在UINavigationViewController中,等等。这不是一个简单的答案,因为有几乎无限的方法可以做到这一点。我建议您熟悉以下文档:。不要只知道如何键入代码,还要理解。