json - My data is being written to Firebase database but i can't read it why? -
i'm new swift , databases. i'm trying read firebase database. i'm using example code provided here: https://www.firebase.com/docs/ios/guide/retrieving-data.html. i'm calling viewcontroller.swift file in override func viewdidload(){}
func getrootref(){ // read data , react changes println("entered getrootref") myrootref.observeeventtype(.value, withblock: { snapshot in println("value1") println(string(stringinterpolationsegment: snapshot.value)) println("value2") }, withcancelblock: { error in println(error.description) println("error") }) println("left getrootref") }
output:
entered getrootref left getrootref viewcontroller3 viewcontroller4 value1 (function) value2
i not understand why "(function)" printing out instead of data. both read , write permission set true. making mac app.
your code sample doesn't give enough info want - however, assuming want print out contents of myrootref node...
i believe in swift, fdatasnapshot.value considered optional - contain value or nil. therefore need unwrap value:
myrootref.observeeventtype(.value, withblock: { snapshot in println( snapshot.value()! ) })
the ! forced unwrap of variable must contain value. should checked nil before printing or else cause runtime error.
this may safer
myrootref.observeeventtype(.value, withblock: { snapshot in if let value: anyobject = snapshot.value() { println(value) } })
note snapshot.value contain 1 of number of different objects: data types returned: * nsdictionary * nsarray * nsnumber (also includes booleans) * nsstring
Comments
Post a Comment