NSFastEnumerationIterator.Element(又名 Any)不符合协议'AnyObject'



升级到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 {

尝试:-

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

最新更新