为什么在添加database.reference时出现错误(预期声明)?

时间:2017-12-18 23:22:18

标签: xcode firebase firebase-realtime-database swift4

我是swift和firebase的新手。但我会尝试同时使用Firebase进行Google登录和电子邮件/密码身份验证。我得到错误:预期的声明。希望有人能帮助我。在我将Google登录代码添加到Xcode之前,它工作正常。

var currenUser: User?

class ViewController: UIViewController, UITextFieldDelegate, GIDSignInUIDelegate  {


    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet var collectionTextField: [UITextField]!

    var dbRef: DatabaseReference!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Google login
        GIDSignIn.sharedInstance().uiDelegate = self
        GIDSignIn.sharedInstance().signIn()
    }

    dbRef = Database.database().reference()

        for textField in self.collectionTextField {

            textField.delegate = self

    }

func loginAction(_ sender: Any) {

    Auth.auth().signIn(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!, completion: {
            (user, error) in

            if let error = error {
                print("Sign in error: \(error.localizedDescription)")
                return
            }

            self.dbRef.child("usersID").child(user!.uid).child("info").setValue(["email": user!.email])

            print("user logged in: \(String(describing: user?.displayName))")

            self.performSegue(withIdentifier: "SignIn", sender: nil)
        })
    }

Error shown here

1 个答案:

答案 0 :(得分:0)

看起来代码在任何函数之外定义dbRef。也许你打算把这些代码放在viewDidLoad()

override func viewDidLoad() {
    super.viewDidLoad()

    // Google login
    GIDSignIn.sharedInstance().uiDelegate = self
    GIDSignIn.sharedInstance().signIn()


    dbRef = Database.database().reference()

        for textField in self.collectionTextField {
            textField.delegate = self
        }

}