Chace
Chace

Reputation: 561

Iterate inside a nested child. Firebase and Swift 3

Here is my data structure:

{ "ItemData": {
    "Item": [
      {
        "name": "Table",
        "measurement ": [
          {
            "height": 30
          },
          {
            "width": 50
          }
        ]
      }
    ]
  }
}

I can currently fetch all the data from Firebase and able to display the name on to a tableView. I am now trying to get the values that are nested inside the 'measurement' i.e. 'height' & 'width'. I have looked at Query Firebase for nested child swift, What's the best way of structuring data on firebase?, Iterate through nested snapshot children in Firebase using Swift and Firebase Swift 3 Xcode 8 - iterate through observe results but still have no clue how to solve this.

This is my Item class:

class Item {

    var name: String!
    var measurement: String!
    var key: String

    init(from snapshot: FIRDataSnapshot) {

        let snapshotValue = snapshot.value as? [String: Any]

        self.name = snapshotValue!["name"] as! String
        self.measurement = snapshotValue?["measurement"] as! String
        self.key = snapshot.key

    }
}

This is the function I use to fetch the item. The ItemManager is a class that has the function to remove and add the array of Item:

func fetchItem() {

    let databaseRef = FIRDatabase.database().reference(withPath: "ItemData/Item/")

    databaseRef.observe(.value, with: { snapshot in

        ItemManager.shared.removeAll()
        for item in snapshot.children {
            guard let snapshot = item as? FIRDataSnapshot else { continue }


            let item = Item(from: snapshot)
            ItemManager.shared.additem(item)
            print(snapshot)
        }

        self.tableView.reloadData()
    })

}

Please help me if you can :)

Upvotes: 1

Views: 1114

Answers (1)

Nirav D
Nirav D

Reputation: 72440

As suggested in comment measurement array of dictionary not the String, So if you want to get height and width from it you can get it this way.

class Item {

    var name: String!
    var heightMeasurement: String!
    var widthMeasurement: String!
    var key: String

    init(from snapshot: FIRDataSnapshot) {

        let snapshotValue = snapshot.value as? [String: Any]

        self.name = snapshotValue!["name"] as! String
        if let array = snapshotValue["measurement"] as? [[String:Any]], 
           let heightDic = array.first, let height = heightDic["height"],
           let widthDic = array.last, let width = widthDic["width"] {            

             self.heightMeasurement = "\(height)"
             self.widthMeasurement = "\(width)"
             print(height, width)
        }
        else {
             self.heightMeasurement = "" //set some default value
             self.widthMeasurement = ""  //set some default value
        }
        self.key = snapshot.key     
    }
}

Note: If your array having more than two objects than to get the height and width you need to subscripting the array index first to get the dictionary and then access its key according to get your value.

Upvotes: 4

Related Questions