0

I'm trying to access all the questions in the tree that starts from

var ref = FIRDatabase.database().reference().child("trivia")

and when I print the contents with the following function

func observeQuestion(completion: @escaping (Trivia) -> Void) {
    ref.observe(.value, with: { snapshot in
        if let dict = snapshot.value {
            print("\(dict)")
        }
    })
}

I get the tree as follows

enter image description here

But I can't figure out how to access the Question and print that out, it always comes out as nil. I want to access all the questions value and store them in an array. How should I approach this?

AL.
  • 36,815
  • 10
  • 142
  • 281
Mohamed Mohamed
  • 3,965
  • 3
  • 23
  • 40

1 Answers1

1

Try this:

let questions = snapshot.value as! [[String: Any]]
for question in questions {
    let title = question["Question"]!
    print("Question: \(title)")
}
Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85