Type “user?” had no member UID (Swift 3, Firebase)

2020-05-01 20:54发布

I am setting up a login page with Firebase and I've run into an error where uid isn't getting recognized. Here's my code:

    FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (FIRUser,error) in

        if error != nil {
            print(error)
            return

        }

        guard let uid = user?.uid else {
            return
        }

I also can't command-click into FIRUser, however if I get rid of the guard statement, authentication works perfectly fine.

I have Firebase and FirebaseDatabase imported (can't import FirebaseAuth, it's crossed out), so it's not that.

Any idea why I can't access FIRUser and use uid?

1条回答
The star\"
2楼-- · 2020-05-01 21:48

You need to create the variable user inside your closure. Rewrite your function to this instead.

FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user: FIRUser?, error: Error?) in

    if error != nil {
        print(error)
        return

    }

    guard let uid = user?.uid else {
        return
    }
查看更多
登录 后发表回答