gpt4 book ai didi

ios - 从 Firebase 获取数据

转载 作者:可可西里 更新时间:2023-11-01 04:50:27 29 4
gpt4 key购买 nike

我正在尝试从 Firebase 获取数据,我试过这样:

FIREBASE_REF.childByAppendingPath("tasks").observeEventType(.Value, withBlock: { (snapshot) -> Void in
print(snapshot.value)
self.tasks = [Task]()
var task = Task()
let data = snapshot.value as! NSDictionary
let tasksFromServer = data.allValues as! [NSDictionary]
for taskFromServer in tasksFromServer {
task.description = taskFromServer.objectForKey("description") as! String
task.startTime = taskFromServer.objectForKey("startTime") as! String
task.endTime = taskFromServer.objectForKey("endTime") as! String
task.progress = taskFromServer.objectForKey("progress") as! Int
let priorityTemp = taskFromServer.objectForKey("priority") as! Int
switch priorityTemp {
case 0: task.priority = .Low
case 1: task.priority = .Medium
case 2: task.priority = .High
default: break
}
task.assignee = taskFromServer.objectForKey("assignee") as! String
self.tasks.append(task)
}
MBProgressHUD.hideAllHUDsForView(self.view, animated: true)
self.tableView.reloadData()
}

但它在这一行显示错误:

let data = snapshot.value as! NSDictionary

它说:无法将类型“__NSArrayM”(0x10ebfc8d8)的值转换为“NSDictionary”(0x10ebfcd60)。

我来自 Firebase 的数据是这样的:

enter image description here

但在另一方面,我在另一个 ViewController 中使用另一个代码从 Firebase 获取用户名和角色,它有效。

FIREBASE_REF.childByAppendingPath("users").observeEventType(.Value, withBlock: { (snapshot) -> Void in
self.names = []
self.roles = []
let data = snapshot.value as! NSDictionary
let employees = data.allValues as! [NSDictionary]
for employee in employees {
let name = (employee.objectForKey("firstName") as! String) + " " + (employee.objectForKey("lastName") as! String)
self.names.append(name)
let role = employee.objectForKey("position") as! String
self.roles.append(role)
}
MBProgressHUD.hideAllHUDsForView(self.view, animated: true)
self.collectionView.reloadData()
})

enter image description here

但为什么第一个代码总是崩溃。

任何帮助将不胜感激。谢谢。

最佳答案

如果超过 50% 的键在 0 和最大键之间(正是您的情况,只有一个零元素),Firebase 会将您的字典对象转换为数组。

https://www.firebase.com/docs/ios/guide/understanding-data.html#section-arrays-in-firebase

关于ios - 从 Firebase 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36217343/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com