I recently upgraded to swift 3 and have been getting an error when trying to access certain things from a snapshot observe event value.
My code:
ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
let username = snapshot.value!["fullName"] as! String
let homeAddress = snapshot.value!["homeAddress"] as! [Double]
let email = snapshot.value!["email"] as! String
}
The error is around the three variables stated above and states:
Type 'Any' has no subscript members
Any help would be much appreciated
When Firebase returns data,
snapshot.value
is of typeAny?
so as you as the developer can choose to cast it to whatever data type you desire. This means thatsnapshot.value
can be anything from a simple Int to even function types.Since we know that Firebase Database uses a JSON-tree; pretty much key/value pairing, then you need to cast your
snapshot.value
to a dictionary as shown below.I think that you probably need to cast your
snapshot.value
as aNSDictionary
.You can take a look on firebase documentation: https://firebase.google.com/docs/database/ios/read-and-write