Rohan Vasishth Rohan Vasishth - 11 months ago 119
iOS Question

Firebase Accessing Snapshot Value Error in Swift 3

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

Answer Source

I think that you probably need to cast your snapshot.value as a NSDictionary.

ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in

    let value = snapshot.value as? NSDictionary

    let username = value?["fullName"] as? String ?? ""
    let homeAddress = value?["homeAddress"] as? [Double] ?? []
    let email = value?["email"] as? String ?? ""


You can take a look on firebase documentation: