【问题标题】:Getting Data from Firebase in Swift 3.0在 Swift 3.0 中从 Firebase 获取数据
【发布时间】:2017-01-23 17:21:26
【问题描述】:

我正在使用带有 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" ]
    }
}

【问题讨论】:

  • 你能提供你的最小JSON,因为文本不是sn-p
  • @Dravidian 当然,完成
  • 你解决了这个问题吗?
  • @André Yea,刚刚发布了答案
  • 感谢 Eliko,刚刚在这里找到了类似的解决方案。

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


【解决方案1】:

我刚刚修复了它投射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
}

【讨论】:

  • 好的,我试过了,我在第 3 行收到警告:Comparing non-optional value of type '[String : AnyObject]' to nil always returns true。顺便说一句,我应该把“狗”放在哪里?
【解决方案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)
            }

【讨论】:

  • 我的光标没有转到 ref.observe(.childAdded, with: { snapshot in....请帮忙!
【解决方案3】:

它检索所有现有的 firebase 用户数据并存储在 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    相关资源
    最近更新 更多