从Swift 3.0中的Firebase获取数据

时间:2016-09-15 15:43:31

标签: ios swift firebase firebase-realtime-database swift3

我正在使用Firebase的应用。

今天搬到swift 3.0后,Xcode让我改变了这段代码:

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)
            }
        })

到此代码:

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)
            }
        })

但是在第二行它给了我一个错误:

  

无法调用非功能类型的值'Any?!'

这是FireBase中的json数据:

{
  “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" ]
    }
}

3 个答案:

答案 0 :(得分:3)

我刚刚修改了它snapshot.value as! [String:AnyObject]

编辑:

更好的解决方案(使用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
}

答案 1 :(得分:2)

在修复我的代码之后,这是正确的解决方案:

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)
            }

答案 2 :(得分:1)

它在NSDictionary中检索所有现有的firebase用户数据和存储:

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)