Melissa Lim
Melissa Lim

Reputation: 55

I want to get the child of my firebase (not the key) using swift

enter image description here

This is my firebase. I want to retrieve the names "anaesthesia", "musculoskeletal", "options" into an array and print it out. This is my swift code

        ref?.child("mcqbase-159dc").observeSingleEvent(of: .value, with: { (snapshot) in
        if snapshot.exists(){
              for child in snapshot.children {
                let snap = child as! DataSnapshot
                let key = snap.key
                self.categoriesArray.append(key)
                print(self.categoriesArray)
                }
            }
        })

It returns a blank in my terminal. Can anyone help me?

Upvotes: 0

Views: 28

Answers (1)

Picode
Picode

Reputation: 1158

"anaesthesia", "musculoskeletal" and other your database table's names. You can change your structure to :

enter image description here

And your snapshot code must be :

Database.database().reference().child("names").observeSingleEvent(of: .value) { (snapshot) in
  
   //Your code
    }

Upvotes: 1

Related Questions