到目前为止,我已经能够从Firebase检索数据并进行打印。但是,当我尝试附加从Firebase收到的内容时,它不起作用。我尝试将数据放入的数组像空[]
一样继续打印,但是如果我仅打印分配给Firebase上的值的变量,它将起作用。可能会发生什么?
override func viewDidLoad() {
super.viewDidLoad()
// Configure Firebase
// ------------------
if FirebaseApp.app() == nil {
FirebaseApp.configure()
}
getData()
print(currentQuestion)
}
getData()
func getData() -> Void {
let ref = Database.database().reference()
let sorter = Int.random(in: 1..<3)
let fbpathIndex = String(sorter)
ref.child("questions/"+fbpathIndex+"/enunciado").observeSingleEvent(of: .value , with: { (snapshot) in
let currentEnunciado = snapshot.value as! String
self.currentQuestion.append(String(currentEnunciado))
})