【发布时间】:2016-09-23 18:51:54
【问题描述】:
在我将 Xcode 升级到 Xcode 8 之前,我有一个由 Firebase 支持的 iOS 应用程序,它一直运行良好。现在错误显示在以下行中:
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'
在将我的代码转换为 Swift 3.0 后,更改使语法变为:
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。
Swift 3.0 的 Firebase 文档没有更改,那么这里有什么问题?
完整代码块:
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
}
}
})
感谢大家的帮助,不胜感激!
【问题讨论】:
标签: ios firebase firebase-realtime-database swift3