【发布时间】:2021-12-28 22:08:36
【问题描述】:
我正在从 firebase 实时数据库中获取值。我想将这些值存储到一个数组中并在 UITableView 中显示它们。这是正在发生的事情:
我在 viewDidLoad() 函数之前定义了数组,如下所示:
var taskTitles: [Task] = []
在我的viewDidLoad() 函数中,我正在调用另一个函数来生成数组:
override func viewDidLoad() {
super.viewDidLoad()
//Setting the Title for the nav bar
title = "To Do List"
configureNavigationItems()
taskTitles = createArray() // creating the array of tasks in db
tableView.delegate = self
tableView.dataSource = self
}
在这个函数中,我将信息传递给我的班级。任务和任务单元。他们只是在处理任务的标题。
func createArray() -> [Task] {
taskRef = Database.database().reference(withPath: "Tasks")
//getting values from db, storing them in an array.
refHandle = taskRef?.observe(DataEventType.value, with: { snapshot in
for taskSnapshot in snapshot.children {
let nodeA = taskSnapshot as! DataSnapshot
let keyA = nodeA.key
let theTask = Task(title: String(keyA))
self.taskTitles.append(theTask)
print("In the FOR Loop --> ", self.taskTitles)
}
print("outside of FOR Loop --> ", self.taskTitles)
})
print("outside of observe func --> ", taskTitles)
return taskTitles
}
}
但是,它似乎没有将我的项目保存到数组中。我做了一些调试来确定哪里出了问题。希望下图可以澄清一下:
知道是什么问题吗?
【问题讨论】:
标签: arrays swift firebase-realtime-database