Firebase Swift 3.0语法更新? [英] Firebase Swift 3.0 Syntax Update?

查看:91
本文介绍了Firebase Swift 3.0语法更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由Firebase支持的iOS应用程序,在将Xcode升级到Xcode 8之前一直运行良好.现在错误显示在以下行中:

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

Swift 3.0中的错误:Value of type 'NSFastEnumerationIterator.Element' (aka 'Any') has no member 'value'

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

将我的代码转换为Swift 3.0之后,所做的更改使语法变为:

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

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

但是会返回此错误:

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

此外,尝试访问快照值时出现此错误:The Type 'Any' has no subscript members.

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

Swift 3.0的Firebase文档没有更改,那么这里的问题是什么?

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

完整代码块:

 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!

推荐答案

我猜您没有将从事件中检索到的快照转换为任何类型,这可能会使Xcode混淆什么快照甚至属于什么类型?

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{

                    print(name)
                }
                if let zip = child.value["ZIP-CODE"] as? String{

                    print(zip)
                }
                if let state = child.value["STATE"] as? String{

                    print(state)
                }
            }
        }
    })

这篇关于Firebase Swift 3.0语法更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆