5

I am using an app with Firebase.

After moving to swift 3.0 today, Xcode asked me to change this code:

ref.observeEventType(.ChildAdded, withBlock: { snapshot in
            let currentData = snapshot.value!.objectForKey("Dogs")
            if currentData != nil {
            let mylat = (currentData!["latitude"])! as! [String]
            let mylat2 = Double((mylat[0]))
            let mylon = (currentData!["longitude"])! as! [String]
            let mylon2 = Double((mylon[0]))
            let userid = (currentData!["User"])! as! [String]
            let userid2 = userid[0]
            let otherloc = CLLocation(latitude: mylat2!, longitude: mylon2!)
            self.distanceBetweenTwoLocations(self.currentLocation, destination: otherloc, userid: userid2)
            }
        })

to this code:

ref.observe(.childAdded, with: { snapshot in
            let currentData = (snapshot.value! as AnyObject).object("Dogs")
            if currentData != nil {
                let mylat = (currentData!["latitude"])! as! [String]
                let mylat2 = Double((mylat[0]))
                let mylon = (currentData!["longitude"])! as! [String]
                let mylon2 = Double((mylon[0]))
                let userid = (currentData!["User"])! as! [String]
                let userid2 = userid[0]
                let otherloc = CLLocation(latitude: mylat2!, longitude: mylon2!)
                self.distanceBetweenTwoLocations(self.currentLocation, destination: otherloc, userid: userid2)
            }
        })

But in the second line it gives me an error:

Cannot call value of non-function type 'Any?!'

This is the json data in the FireBase:

{
  “user1” : {
    "Dogs" : {
      "User" : [ "siACmQZ7MDclDSO3hrCOp953kfl2" ],
      "latitude" : [ "32.172344" ],
      "longitude" : [ "34.858068" ]
    }
  “user2” : {
    "Dogs" : {
      "User" : [ "siACmQZ7MDclDSO3hrCOp953kfl2" ],
      "latitude" : [ "32.172344" ],
      "longitude" : [ "34.858068" ]
    }
  “user3” : {
    "Dogs" : {
      "User" : [ "siACmQZ7MDclDSO3hrCOp953kfl2" ],
      "latitude" : [ "32.172344" ],
      "longitude" : [ "34.858068" ]
    }
}
AL.
  • 36,815
  • 10
  • 142
  • 281
Eliko
  • 1,137
  • 4
  • 16
  • 26

3 Answers3

3

I just fixed it casting snapshot.value as! [String:AnyObject]


EDIT:

Better solution (using SwiftyJSON):

func parseFirebaseResponse(snapshot: FIRDataSnapshot) -> [JSON] {
    var returnJSON: [JSON] = []
    let snapDict = snapshot.value as? [String:AnyObject]
    for snap in snapshot.children.allObjects as! [FIRDataSnapshot] {
        let json = snapDict?[snap.key]
        returnJSON.append(JSON(json as Any))
    }
    return returnJSON
}
f0go
  • 262
  • 2
  • 7
  • Ok I tried it and im getting a warning in the 3rd line: `Comparing non-optional value of type '[String : AnyObject]' to nil always returns true`. Btw, where Do I put "Dogs"? – Eliko Sep 17 '16 at 00:20
2

This is the right solution, after fixing my code:

ref.observe(.childAdded, with: { snapshot in
            if let snapshotValue = snapshot.value as? [String:Any],
                let currentData = snapshotValue["Dogs"] as? [String:Any] {
                let userid = (currentData["User"])! as! [String]
                let userid2 = userid[0]
                let mylat = currentData["latitude"] as! [String]
                let mylat2 = Double((mylat[0]))
                let mylon = (currentData["longitude"])! as! [String]
                let mylon2 = Double((mylon[0]))
                let otherloc = CLLocation(latitude: mylat2!, longitude: mylon2!)
                self.distanceBetweenTwoLocations(self.currentLocation, destination: otherloc, userid: userid2)
            }
Eliko
  • 1,137
  • 4
  • 16
  • 26
1

It retrieves all existing firebase users data and stores in NSDictionary:

let ref = FIRDatabase.database().reference(fromURL: "DATABASE URL")
let usersRef = ref.child("users").observeSingleEvent(of: .value, with: {(snapshot) in
print(snapshot)
let Dict = snapshot.value as! NSDictionary   //stores users data in dictionary(key/value pairs)
print(Dict)
urvashi bhagat
  • 1,123
  • 12
  • 15