简体   繁体   中英

Swift writing data into Firebase Error Expected Declaration

I just started learning Swift and I have an issue with writing into my Real Time Database. I followed the Firebase docs but still have this problem.

Here is my View Controller:

import UIKit
import FirebaseDatabase

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

    }
        var ref: DatabaseReference! = Database.database().reference()

        self.ref.child("Produkt/Kappe/Laden").setValue("Netto") //this is where I am getting the error: "Expected declaration"
}

Please help. Thanks

Use this inside viewdidload or any function because you cannot perform these actions outside function scope

Try this

 class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
            self.ref.child("Produkt/Kappe/Laden").setValue("Netto")

}
    var ref: DatabaseReference! = Database.database().reference()


}

you cannot do this outside any function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM