ios – 从Swift 3.0中的Firebase获取数据

ios – 从Swift 3.0中的Firebase获取数据,第1张

概述我正在使用Firebase的应用程序. 在今天转移到swift 3.0之后,Xcode让我改变了这段代码: ref.observeEventType(.ChildAdded, withBlock: { snapshot in let currentData = snapshot.value!.objectForKey("Dogs") if curre 我正在使用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,userID: userID2)            }        })

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

Cannot call value of non-function type ‘Any?!’

这是FireBase中的Json数据

{  “user1” : {    "Dogs" : {      "User" : [ "siACmQZ7MDclDSO3hrcop953kfl2" ],"latitude" : [ "32.172344" ],"longitude" : [ "34.858068" ]    }  “user2” : {    "Dogs" : {      "User" : [ "siACmQZ7MDclDSO3hrcop953kfl2" ],"longitude" : [ "34.858068" ]    }  “user3” : {    "Dogs" : {      "User" : [ "siACmQZ7MDclDSO3hrcop953kfl2" ],"longitude" : [ "34.858068" ]    }}
解决方法 我刚刚修改了它,将snapshot.value定为! [字符串: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}
总结

以上是内存溢出为你收集整理的ios – 从Swift 3.0中的Firebase获取数据全部内容,希望文章能够帮你解决ios – 从Swift 3.0中的Firebase获取数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1072997.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存