I'm trying to do a firebase read, I'm trying to read an integer from firebase, but for some reason this isn't working:
func firebaseCall () {
ref.observeEventType(.Value, withBlock: { snapshot in
print(snapshot.value)
let people = snapshot.value["people"] as! Int
print("People is \(people)")
// let time = snapshot.value["time"] as! Int
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.Hour, fromDate: date)
let hour = components.hour
print("time is \(hour)")
}, withCancelBlock: { error in
print(error.description)
})
}
I get the error Ambiguous use of subscript on the snapshot.value["people"]. I used the exact code in another project and it works so I'm incredibly confused. The difference is that this code is used in a SKScene where as the other time it was in a view controller. Can anyone help fix this? Or suggest another way i can read the int value from firebase? Thanks
The compiler doesn't know what is being returned by
let people = snapshot.value["people"] as! Int
You may want to consider
if let someInt = snapshot.value.objectForKey("people")
or
if let someInt = snapshot.value as? Int
Not knowing what is in the snapshot leaves a little ambiguity.
If the object may be nil you may want to add some way to handle that
if let thing = snapshot.value {
print("thing was not nil")
} else {
print("thing was nil")
}