marq marq - 1 month ago 8
iOS Question

How to set variables from a Firebase snapshot (swift)

I would like to grab the data from my firebase snapshot and set it to variables in my Project. I get the data, but I don't know how to parse it so I can set them. I'm trying to do something like what SwiftyJSON does with API calls.

Here is my data structure in firebase: data structure



Here is my function: function



Here is the output in the console: output



Is there any way to parse and set values from the (ls) array and set the "fromId", "text", "toId" to different variables for each node?

Answer

Try using:-

FIRDatabase.database().reference().child("users/userID/ls").observerSingleEvent(of : .value, with : {(Snap) in 

  if let snapDict = Snap.value as? [String:AnyObject]{

      for each in snapDict{

           print("\(each.value["fromId"] as! String)")

         }
   }
})