My database looks like this
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
I would like to retrieve the value of the "storage" key in a specific subcategory node. I've tried tapping into one specific subcategory and retrieve to storage key with the following code:
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)
}
This code crashes the app on the line let dict = imageSnap.value as! [String: Any]
with the error code Could not cast value of type '__NSCFString' (0x10632a168) to 'NSDictionary' (0x10632b1a8)
I've tried printing snapshot, snapshot.child, snapshot.value etc. None of these give me what I'm looking for, they either do not work, or give me one large dictionary containing subCat1, all items, someData and storage.
Update:
with the help of Sh_Khan's answer below, I'm able to print the storage strings for each item in the node, however my for in loop causes the all the strings to be printed 3 times each so I get a total of 3 x 3 urls when I only want 3 (one for each item)