Firebase iOS Swift从其他节点获取数据的收藏夹列表
问题描述:
Pictures
-pictureID
-- name
-- date
Like
-pictureID
-- userID: true
-- userID: true
likePerUser
-userID
--pictureID: true
--pictureID: true
Users
-userID
-- name
-- lastname
我想检索当前用户喜欢的所有图片。Firebase iOS Swift从其他节点获取数据的收藏夹列表
我所做的:
ref.child("likePerUser").child(FIRAuth.auth()!.currentUser!.uid).observeSingleEvent(of: .value, with: { (snap) in
for item1 in snap.children{
let firstItem1 = (snap: item1 as! FIRDataSnapshot)
print("key favourites\(firstItem1.key)")
let firstId = firstItem1.key
self.ref.child("pictures").child(firstId).observeSingleEvent(of: .value, with: { (snapshot) in
for item in snapshot.children{
let firstItem = (snapshot: item as! FIRDataSnapshot)
print("key pictures \(firstItem.key)")
let dict = firstItem.value as! [String: Any
let name = dict["name"] as! String
print(name)
}
甚至,如果它似乎firstId每次 具有正确的值,我总是得到一个错误:
Could not cast value of type '__NSCFBoolean' (0x110dae5b8) to 'NSDictionary' (0x110daf288).
请帮助....
答
我解决这样做:
if let dictionary = snapshot.value as? NSDictionary {
if let name = dictionary["nome"] as? String {
print(name)
}
}
这个问题,让我:Swift - Could not cast value of type '__NSCFString' to 'NSDictionary'
此外,我也没必要重复一次。
答
let dict = firstItem.value as! [String: Any
这里是您尝试将值转换为字典的位置。目前还不清楚该值是否是基于您共享的数据库模型的字典。但是,如果您打印firstItem.value.debugDescription
,您很可能会看到它的值不是字典对象。
如果你想得到一些帮助,修复你的代码并告诉我们错误发生在哪里。 – SahandTheGreat