【发布时间】:2020-04-22 00:27:22
【问题描述】:
我的数据库是这样的
category
subCat1
item1
someData: somedata
storage: storageref
item2
someData: somedata
storage: storageref
item3
someData: somedata
storage: storageref
subCat2
item1
someData: somedata
storage: storageref
item2
someData: somedata
storage: storageref
item3
someData: somedata
storage: storageref
我想检索特定子类别节点中“存储”键的值。我尝试使用以下代码进入一个特定的子类别并检索到存储密钥:
let ref = Database.database().reference().child("category").child("subCat1")
ref.observeSingleEvent(of: .value) { (snapshot) in
for item in snapshot.children {
let snap = item as! DataSnapshot
let imageSnap = snap.childSnapshot(forPath: "storage")
let dict = imageSnap.value as! [String: Any]
let url = dict["storage"] as! String
print(url)
}
此代码使应用程序在let dict = imageSnap.value as! [String: Any] 行崩溃,错误代码为Could not cast value of type '__NSCFString' (0x10632a168) to 'NSDictionary' (0x10632b1a8)
我尝试过打印快照、快照.child、快照.值等。这些都没有给我我想要的东西,它们要么不起作用,要么给我一个包含 subCat1、所有项目、someData 的大字典和存储。
更新:
在下面 Sh_Khan 的回答的帮助下,我可以打印节点中每个项目的存储字符串,但是我的 for in 循环导致所有字符串每次打印 3 次,所以我总共得到 3 x 3 个 url,当我只想要 3 个时(每个项目一个)
【问题讨论】:
标签: swift firebase firebase-realtime-database