2017-04-23 34 views
0
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

回答

0
let dict = firstItem.value as! [String: Any 

這裏是您嘗試將值轉換爲字典的位置。目前還不清楚該值是否是基於您共享的數據庫模型的字典。但是,如果您打印firstItem.value.debugDescription,您很可能會看到它的值不是字典對象。

+0

日Thnx的答案,如果我打印的內容,你建議我我得到: 可選(名稱) 可選(日期) 每一個畫面我喜歡,所以我看這不是一本字典,但如何才能獲得,如果這些值我無法迭代字典? – HaVaNa7

+0

如果您更新了問題以顯示更完整的數據庫模型,它將對我有所幫助。例如,您的數據庫模型不會顯示名爲「favperuser」的子項,但您在代碼中使用了一個。無論如何,請檢查代碼的大小寫和拼寫,以確保它與數據庫中路徑的名稱完全一致。 –

+0

我改正了,thnx – HaVaNa7

相關問題