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?

1 Answer 1

1

Try this:

let questions = snapshot.value as! [[String: Any]]
for question in questions {
    let title = question["Question"]!
    print("Question: \(title)")
}
Sign up to request clarification or add additional context in comments.

3 Comments

I think that approach would work. Maybe OP could look at this answers swell: stackoverflow.com/questions/42599617/…
Thanks @J.Doe! Maybe this might also be helpful.
Yes! Thank you Mr. Mattos!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.