Ryan Ryan - 4 months ago 294
iOS Question

Firebase Swift 3.0 Syntax Update?

I have had an Firebase-backed iOS app that has been working well until I upgraded Xcode to Xcode 8. Now errors show up in lines such as:

let state = child.value!["STATE"] as! String // Was correct in Swift 2.3

Error in Swift 3.0:
Value of type 'NSFastEnumerationIterator.Element' (aka 'Any') has no member 'value'

After Converting my code to Swift 3.0, the change made the syntax into this:

let name = (child as AnyObject).value!["NAME"] as! String

But that returns this error:

Type 'NSFastEnumerationIterator.Element' (aka 'Any') does not conform to protocol 'AnyObject'

Additionally, I am getting this error:
The Type 'Any' has no subscript members
when I try to access a snapshot value.

Firebase docs have not changed for Swift 3.0, so what is the issue here?

Full Code Block:

self.firebase.child(“INFO”).observeSingleEvent(of: .value, with: { (snap: FIRDataSnapshot) in

for child in snap.children{

if child.hasChild("NAME") && child.hasChild("ZIP-CODE") && child.hasChild("STATE"){

let name = child.value!["NAME"] as! String
let zip = child.value!["ZIP-CODE"] as! String
let state = child.value!["STATE"] as! String



Thanks for all help, it is greatly appreciated!


I am guessing you are not casting your retrieved Snapshot from an event to any type, Which might lead the Xcode confused as to what type does this Snapshot even belong to?

   FIRDatabase.database().reference().observeSingleEvent(of : .value, with : {(Snapshot) in

        if let snapDict = Snapshot.value as? [String:AnyObject]{
            for child in snapDict{

                if let name = child.value["NAME"] as? String{

                if let zip = child.value["ZIP-CODE"] as? String{

                if let state = child.value["STATE"] as? String{