gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 16:12:47 28 4
gpt4 key购买 nike

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

关于swift - NSFastEnumerationIterator.Element(又名 Any)不符合协议(protocol) 'AnyObject',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39380205/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com