我正在尝试从Firebase中的JSON数据库中检索名字。数据库的结构如下
users
|_Unique ID
|_firstName
|_lastName
|_userEmail
|_userID (Which I copied to the database from FirebaseAuth account creation)
我正在尝试在欢迎标签中显示firstName。以下是我设置视图控制器的方法(免责声明,我是一名慢慢学习如何在XCode中工作的设计师)
class HomeVC : UIViewController {
let rootRef = FIRDatabase.database().reference()
@IBOutlet weak var WelcomeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let userID = (FIRAuth.auth()?.currentUser?.uid)!
rootRef.child("users").child(userID).observeSingleEventOfType(
.Value, withBlock: { (snapshot) in
let firstName = snapshot.value!["firstName"] as! String
self.WelcomeLabel.text = firstName
})
}
}
我一直收到一条错误消息“致命错误:在展开Optional值时意外发现nil”。有任何想法吗?我碰巧错误地构建了数据库吗?