我正在遵循指南,我的目标是能够拥有两个文本字段,这些文本字段可以将用户的名称和电子邮件保存到Firebase数据库或存储中。我遇到了很多错误,想知道如何对代码进行修复,或者是否有更好的指南可以实现我的目标。
https://medium.com/@felicity.johnson.mail/firebase-user-sign-up-login-data-management-992d778b167
import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
class ViewController: UIViewController {
@IBOutlet weak var textFieldName: UITextField!
@IBOutlet weak var textFieldEmail: UITextField!
@IBOutlet weak var labelNotification: UILabel!
@IBAction func buttonAddSave(_ sender: UIButton) {
let ref = Database.database().reference().root
let key = ref.child("names").childByAutoId().key
guard let userKey = Auth().currentUser?.uid else {return}
ref.child("emails").child(userKey).observeSingleEvent(of: .value, with: { snapshot in
var count = "0"
if let value = snapshot.value as? [String] {
count = String[describing: value.count]
}
var newName = [String: String]()
newName[count] = key
ref.child("names").child(userKey).updateChildValues(newName)
self.presentAlertWithTitle(title:"Congrats!", message: "You successfully joined the Mobile Development Club!")
})
}
答案 0 :(得分:1)
好吧,让我们分别进行每个步骤。
首先,您的用户是否获得认证?因为如果他不是, 这条线
guard let userKey = Auth().currentUser?.uid else {return}
将始终返回。
count = String[describing: value.count]
应该是
count = String(describing: value.count)
self.presentAlertWithTitle(title:"Congrats!", message: "You successfully joined the Mobile Development Club!")
可能甚至没有在您的代码中实现-而且它不是UIViewController
函数。
我认为您对要尝试实现的所有 Firebase函数感到困惑,因此建议您只找到一个更好的教程。我可以推荐您this one-这是官方。