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).

请帮助....

+0

如果你想得到一些帮助,修复你的代码并告诉我们错误发生在哪里。 – SahandTheGreat

我解决这样做:

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,您很可能会看到它的值不是字典对象。

+0

日Thnx的答案,如果我打印的内容,你建议我我得到: 可选(名称) 可选(日期) 每一个画面我喜欢,所以我看这不是一本字典,但如何才能获得,如果这些值我无法迭代字典? – HaVaNa7

+0

如果您更新了问题以显示更完整的数据库模型,它将对我有所帮助。例如,您的数据库模型不会显示名为“favperuser”的子项,但您在代码中使用了一个。无论如何,请检查代码的大小写和拼写,以确保它与数据库中路径的名称完全一致。 –

+0

我改正了,thnx – HaVaNa7