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

无人久伴 提交于 2019-12-04 06:11:41

问题


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:


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
    }


来源:https://stackoverflow.com/questions/40386771/type-user-had-no-member-uid-swift-3-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!