错误是类型'AuthDataResult'的值没有成员'uid'吗?

时间:2018-08-23 03:04:13

标签: swift firebase firebase-authentication

My codes error 我不知道为什么说“ uid”没有成员,我也不知道AuthDataResult的来源,因为我从未将其用作导入或类似的东西……

@IBAction func signinPressed(_ sender: Any) {
    if let email = emailField.text, let password = passwordField.text {
        Auth.auth().signIn(withEmail: email, password: password) { (user, error )
        in
            if error != nil{
                //create account
            } else {

                KeychainWrapper.standard.set((user?.uid)!,
                forKey: ("Key_UID"))
                self.preformSegue(performSegue(withIdentifier: "toFeed", sender: nil))
            }
    }
}
}

1 个答案:

答案 0 :(得分:1)

您得到的错误是:

  

类型AuthDataResult的值没有成员uid

如果您查看reference documentation for AuthDataResult,您会发现这是正确的:该类中没有uiduid中存在FIRUser属性,因此您需要使用:

user?.user.uid

或者为了减少混乱,请为您当前的user变量命名,使其更匹配:

Auth.auth().signIn(withEmail: email, password: password) { (authData, error ) in
    if error != nil{
        //create account
    } else {

        KeychainWrapper.standard.set((authData?.user.uid)!,
        forKey: ("Key_UID"))
        self.preformSegue(performSegue(withIdentifier: "toFeed", sender: nil))
    }
}