bao le
bao le

Reputation: 1415

Retrieving data from firebase returned nil

Below is the code I tried to test.

Tree:

{
  "WorkingTime" : {
    "CloseTime" : 20,
    "SpaceTime" : 30,
    "StartTime" : 7
  }
}
override func viewDidLoad(){
        super.viewDidLoad()
        ref = Database.database().reference()
        handel = ref.child("WorkingTime").observe(.value, with: { snapshot in
            if let dict = snapshot.value as? [String:Any]{
                let startTimeFB = dict["StartTime"] as? String
                let endTimeFB = dict["CloseTime"] as? String
                print("\(startTimeFB ?? "nill") and \(endTimeFB ?? "nill")"
            }
        })
}

Apparently, it printed nill and nill

Upvotes: 1

Views: 52

Answers (1)

Kamran
Kamran

Reputation: 15268

Key name and type cast seems to be the two problems. Please try the following code,

override func viewDidLoad(){
        super.viewDidLoad()

        ref = Database.database().reference()

        handel = ref.child("WorkingTime").observe(.value, with: { snapshot in
            if let dict = snapshot.value as? [String:Any]{
                let startTimeFB = dict["StartTime"] as? Int
                let closeTimeFB = dict["CloseTime"] as? Int
                print("\(startTimeFB ?? 0) and \(closeTimeFB ?? 0)"
            }
        })
}

Upvotes: 2

Related Questions