1

I'm new to firebase and swift in general, and I was really confused on how to access information from a firebase database. So basically, I put in this data into my database:my database

I did all the setup and everything in xcode. However, I've been clueless on how to access this data? I have a model like this: enter image description here

I looked through the firebase documentation and it seems you can get a NSDictionary from the snapshot.value, so that's what I did here:

var ref:FIRDatabaseReference!
var handle:FIRDatabaseHandle!

override func viewDidLoad() {
    super.viewDidLoad()

    ref = FIRDatabase.database().reference()


    ref?.child("series").observe(.value, with: { (snapshot) in
        let dict1 = snapshot.value as? NSDictionary
        let value1 = dict1?.allValues
        for i in 0...(snapshot.childrenCount - 1)
        {
            //???
        }
    })

But as far as I know, NSDictionary isn't an array of objects, and you can't call an index and a property from the object at that index, like arrayofobjects[3].property, so I want to transfer it with for loops to an array of objects of type Model so that I can use the information in my app. Is that how you're supposed to do it? How do I transfer it to an array of objects when you can't take an index of NSDictionary?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Ethan Zhao
  • 229
  • 1
  • 6
  • 18
  • 3
    1. Why post some of your code as images instead of as text? Post all relevant code as text. 2. Why use `NSDictionary` in Swift? Use a Swift dictionary instead. – rmaddy Apr 13 '17 at 15:21
  • 1
    @rmaddy what's the difference? – Ethan Zhao Apr 13 '17 at 15:25
  • The difference with what? – rmaddy Apr 13 '17 at 15:25
  • 2
    I'd wait converting to a dictionary as long as possible. First access the collection of children with `snapshot.children` and loop over it like shown [here](http://stackoverflow.com/a/37581936/209103). Then get access to each individual property with `snapshot.value`. – Frank van Puffelen Apr 13 '17 at 15:26
  • Oh, I see. So you can access each individual property in the children? @FrankvanPuffelen – Ethan Zhao Apr 13 '17 at 15:33
  • 1
    Firebase has great [tutorial](https://firebase.google.com/docs/database/ios/read-and-write), so just save your time and just read it. – Jurasic Apr 13 '17 at 15:50
  • @Jurasic I read that, it just made me more confused. – Ethan Zhao Apr 13 '17 at 19:02

0 Answers0