【问题标题】:NSFastEnumerationIterator.Element (aka Any) does not conform to protocol 'AnyObject'NSFastEnumerationIterator.Element(又名 Any)不符合协议“AnyObject”
【发布时间】:2016-09-07 22:58:48
【问题描述】:

升级到 Xcode 8 (Swift 3) 后,当我尝试在快照中单步执行子记录时,我的 Firebase 查询出现错误 NSFastEnumerationIterator.Element (aka Any) does not conform to protocol 'AnyObject'

for child in snapshot.children {
                if (child as AnyObject).value!["postedBy"] != nil { 

Xcode 将 child.value["postedBy"] 更改为 (child as AnyObject).value!["postedBy"],这会引发错误。然后我尝试将其更改为

((child as AnyObject).value as? NSDictionary)["postedBy"] != nil 

但随后会引发不同的错误Binary operator != cannot be applied to operands of type _ and _

我的方向正确吗?任何帮助将不胜感激。

谢谢!!

最终解决方案:

for child in snapshot.children{
                if let postedBy = (snapshot.value as? NSDictionary)?["postedBy"] as? String {

【问题讨论】:

    标签: swift firebase firebase-realtime-database swift3 xcode8


    【解决方案1】:

    试试看:-

      for child in snapshot.children{
                if let postedBy = child.value!["postedBy"] as? String { .. }}
    

    【讨论】:

    • 第一行出现错误:Cannot convert value of type 'NSEnumerator' to type 'NSDictionary in coercion
    • 我想是的!我不得不把它改成if let postedBy = (snapshot.value as? NSDictionary)?["postedBy"] as? String {谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    • 2015-01-02
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多