james james - 3 months ago 213
Swift Question

NSFastEnumerationIterator.Element (aka Any) does not conform to protocol 'AnyObject'

After upgrading to Xcode 8 (Swift 3), my Firebase queries have the error

NSFastEnumerationIterator.Element (aka Any) does not conform to protocol 'AnyObject'
when I try to step through child records in a snapshot:

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


Xcode changed
child.value["postedBy"]
to
(child as AnyObject).value!["postedBy"]
which throws the error. I then tried changing it to

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


but then it throws a different error
Binary operator != cannot be applied to operands of type _ and _


Am I going in the right direction? Any help would be greatly appreciated.

Thanks!!

Final solution:

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

Answer

Try :-

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