NSFastEnumerationIterator.Element(又名 Any)不符合协议“AnyObject”
Posted
技术标签:
【中文标题】NSFastEnumerationIterator.Element(又名 Any)不符合协议“AnyObject”【英文标题】:NSFastEnumerationIterator.Element (aka Any) does not conform to protocol '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
【问题讨论】:
【参考方案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
谢谢!!以上是关于NSFastEnumerationIterator.Element(又名 Any)不符合协议“AnyObject”的主要内容,如果未能解决你的问题,请参考以下文章
类型“NSFastEnumerationIterator.Element”(又名“Any”)不符合协议“AnyObject”