2016-09-25 91 views
0

我目前正在關注Udemy課程,該課程將教您如何使用Firebase創建聊天應用程序。然而,幾周前我完成了這門課程,然後突然發佈了Swift 3.0更新。我現在努力這條線轉換成斯威夫特3火力地堡:Swift Firebase snapshot.allValues更新

firebase.child("Recent").queryOrderedByChild("chatRoomID").queryEqualToValue(chatRoomID).observeSingleEventOfType(.Value) { (snapshot:FIRDataSnapshot) in 


     var createRecent = true 

     if snapshot.exists() { 
      for recent in snapshot.value!.allValues { 
       if recent["userId"] as! String == userId { 
        createRecent = false 
       } 
      } 
     } 

     if createRecent { 

      CreateRecentItem(userId, chatRoomID: chatRoomID, members: members, withUserUsername: withUserUsername, withUserUserId: withUseruserId) 


     } 

    } 

我試着這樣做:

firebase.child("Recent").queryOrdered(byChild: "chatRoomID").queryEqual(toValue: chatRoomID).observeSingleEvent(of: .value) { (snapshot:FIRDataSnapshot) in 

     var createRecent = true 

     if snapshot.exists() { 

      if let values = snapshot.value as? [String:AnyObject] { 

       for recent in values { 
        if recent["userId"] as! String == userId { 

        } 
       } 
      } 

       //} 

      } 

     } 

    } 

但是,當然,這將返回一個錯誤。任何想法,我將如何解決這個特定的代碼轉換?

在此先感謝。

+0

提起錯誤的錯誤和線 – Dravidian

回答

3

嘗試使用: -

firebase.child("Recent").queryOrdered(byChild: "chatRoomID").queryEqual(toValue: chatRoomID).observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in 

     var createRecent = true 

     if snapshot.exists() { 

      if let values = snapshot.value as? [String:AnyObject] { 

       for recent in values { 
        if let userId = recent.value["userId"] as? String{ 

         } 
       } 
      } 
     } 

    }) 
+0

謝謝!有效! – askaale