Atakan Cavuslu Atakan Cavuslu - 2 months ago 10
Swift Question

How to make an array from child nodes?

I'm using Firebase for my Swift iOS application. I tried the Firebase tutorials but still no luck for me. I need to make an array from the names of Topics in my

/topics
child.

My Firebase data looks like this:

topics
Topic 1
Entry 1
point: 1
user: user1
Entry 2
point: 2
user: user2


I need to make an array consisting of [
Topic 1
,
Topic 2
, ...]. How can I make this array happen? Maybe I'm nesting the data in the incorrect way? Any help would be appreciated.

I also tried to retrieve the data but
snapshot
is a type of
<< error type >>
so I cannot get any value out of it? What might be the problem?

let ref = FIRDatabase.database().reference().child("topics")

ref.observeEventType(.Value) { (snapshot) in

print(snapshot)

}

M_G M_G
Answer

Convert the keys of the snapshot value into an array:

refHandle = postRef.observe(FIRDataEventType.value, with: { (snapshot) in
  let topicsDict = snapshot.value as! [String : AnyObject]
  topicsArray = Array(topicsDict.keys)
})