Firebase: How to get User.uid?

99封情书 提交于 2019-11-29 20:02:05

问题


I'm using Firebaseauth to manage users and data for my iOS app. The users can log in, and their userinfo is stored correct in the database. But when I try to get the users to write to the database in a different viewController, using this string:

self.ref.child("users").child(user.uid).setValue(["username": username])

The thrown error is

Type user has no member .uid

That makes sense I guess, since I haven't created the variable. But I can't figure out how to declare it?


回答1:


This is how to get users uid:

let userID = Auth.auth().currentUser!.uid



回答2:


You might want to use a guard, as a force unwrap might break your app if a user isn’t signed in.

guard let userID = Auth.auth().currentUser?.uid else { return }



回答3:


FIRAuth has actually been renamed to Auth. So, as per the latest changes, it will be

let userID = Auth.auth().currentUser!.uid

Edit.

As pointed out in comments, A force unwrap may crash the app.

guard let userID = Auth.auth().currentUser?.uid else { return }


来源:https://stackoverflow.com/questions/44391706/firebase-how-to-get-user-uid

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