如何从函数readFireBaseData()中获取self.valve1的值到函数viewDidload()?我总是得到" valve1"的设定值。这是我在计划开始时设定的。希望有人可以帮助我。
import UIKit
import FirebaseDatabase
import Firebase
class ValveViewController: UIViewController {
var valve1 = "OFF"
@IBOutlet var valveOne: UILabel!
override func viewDidLoad() {
readFirebaseData() //run the function readFireBaseData
let toggle = view.viewWithTag(1) as! UISwitch
if valve1 == "ON"
{
toggle.setOn(true, animated: true)
}else
{
toggle.setOn(false, animated: true)
}
super.viewDidLoad()
}//END of viewDidLoad
func readFirebaseData(){
var ref: DatabaseReference!
ref = Database.database().reference()
ref.child("switchValve").observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
self.valve1 = value?["valveOne"] as? String ?? ""
})
}//End of readFireBaseData
}//End of ViewController