Firebase Swift 3.0语法更新?

编程入门 行业动态 更新时间:2024-10-22 07:19:47
本文介绍了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语法更新?

本文发布于:2023-11-26 17:07:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634464.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语法   Firebase   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!