0

I've updated Xcode to version 8 and now there is some problem with Firebase. This is a piece of code:

let target = snapshot.value!["target"] as! NSDictionary
self.myZodiac.text = snapshot.value!["zodiac"] as! String
let nsHeight = snapshot.value!["height"] as! NSNumber

// Type 'Any' has no subscript members

In Swift 2.3 all of this works! how to fix it?

One more:

var messagesDictionary = [[String:Int]]()
userRef.observe(FIRDataEventType.value, with: { (snapshot) in

    for item in snapshot.children.allObjects {
        for itemDic in self.messagesDictionary {
            for (key,value) in itemDic {
                if (item as AnyObject).key == key {
                    var photo = UIImage()
                    let age = (item as AnyObject).value!["age"] as! NSNumber as Int //error Type 'Any' does not conform to protocol 'AnyObject'
                    let name = (item as AnyObject).value!["name"] as! String //error Type 'Any' does not conform to protocol 'AnyObject'
                    if (item as AnyObject).hasChild("avatar"){
                        let avatar = (item as AnyObject).value!["avatar"] as! String //error Type 'Any' does not conform to protocol 'AnyObject'
                        self.storageRef.child(key).child(avatar).data(withMaxSize: 5 * 1024 * 1024, completion: { (data, error) -> Void in
                        if (error != nil) {

                        } else {
                            photo = UIImage(data:data!)!

                        }
                        })
                        ////
                        }else{
                            photo = UIImage(named: "no_avatar")!


                    }

                }

            }
        }
    }


})

first example i used:

let target = (snapshot.value as? NSDictionary)?["target"] as! NSDictionary
self.myZodiac.text = (snapshot.value as? NSDictionary)?["zodiac"] as! String
let nsHeight = (snapshot.value as? NSDictionary)?["height"] as! NSNumber

now what to do with item as AnyObject from second piece of code?

adjuremods
  • 2,938
  • 2
  • 12
  • 17
Denis Windover
  • 445
  • 2
  • 6
  • 20

2 Answers2

1

FIRDataSnapshot.value is of type Any, so you can't simply subscript it.

The solution is to first downcast the value to a dictionary:

ref!.observe(.value, with: { (snapshot) in
    for child in snapshot.children {
        let msg = child as! FIRDataSnapshot
        print("\(msg.key): \(msg.value!)")
        let val = msg.value! as! [String:Any]
        print("\(val["name"]!): \(val["message"]!)")
    }
})

From my answer here: Ambiguous Use of Subscript (Swift 3)

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I've seen people use `child in snapshot.children` and also `child in snapshot.children.allObjects` - what is the difference? – fatfrog Nov 11 '16 at 20:39
0

Try this:-

if let snapDict = snapshot.value as? [String:AnyObject]{

     let target = snapDict["target"] as! [String:AnyObject]
     self.myZodiac.text = snapDict["zodiac"] as! String
     let nsHeight = snapDict["height"] as! Float

      }
Dravidian
  • 9,945
  • 3
  • 34
  • 74