【问题标题】:I get the node Key, not the Child key from snapshot Swift 4我从快照 Swift 4 中获取节点密钥,而不是子密钥
【发布时间】:2019-03-02 11:39:42
【问题描述】:

我正在从 Firebase 数据库中检索节点中的帖子,但我得到的是 Node 键而不是 Child 键,我将使用它从数据库中删除条目。我得到键值 I guard let firebaseKey = snapshot.key as? String else { return } 的行。我试过snapshot.children,但没有key参数可供选择。那我该怎么做呢? 像往常一样非常感谢。

完整的功能是:

func displayAlerts(setCompletion: @escaping (Bool) -> ()) {
        print("                     MapArray.alertNotificationCoordinatesArray before displayAlert snapshot is: \(MapArray.alertNotificationCoordinatesArray)")
        print("                     self.userAlertNotificationArray before displayAlert snapshot is: \(self.userAlertNotificationArray)")

        if self.userAlertNotificationArray.count == 0 {
           ref = Database.database().reference()

            ref?.child("Continent").child("Europe").child("Country").child("Italy").child("Region").child("Emilia-Romagna").child("City").child("Bologna").child("Community").child("Alert Notifications").observeSingleEvent(of: .value, with: { (snapshot) -> Void in
                print("         snapshot is: \(snapshot)")
                guard let data = snapshot.value as? [String :[String:String]] else { return }

                guard let firebaseKey = snapshot.key as? String else { return }

                data.values.forEach {
//                    let firebaseKey = data.keys[]

                    let dataLatitude = $0["Latitude"]!
                    let dataLongitude = $0["Longitude"]!
                    let type = $0["Description"]!
                    let id = Int($0["Id"]!)

                    print("firebaseKey is:\(firebaseKey)")
                    print("dataLatitude is: \(dataLatitude)")
                    print("dataLongitude is: \(dataLongitude)")
                    print("type is: \(type)")
                    print("id is: \(id)")
                    print("Key is: \(firebaseKey)")
                    print("data is: \(data)")

                    let doubledLatitude = Double(dataLatitude)
                    let doubledLongitude = Double(dataLongitude)
                    let recombinedCoordinate = CLLocationCoordinate2D(latitude: doubledLatitude!, longitude: doubledLongitude!)

                    let userAlertAnnotation = UserAlert(type: type, coordinate: recombinedCoordinate, firebaseKey: firebaseKey, title: type,id: id!)
                    self.userAlertNotificationArray.append(userAlertAnnotation)  // array of notifications coming from Firebase
                    MapArray.alertNotificationCoordinatesArray.append(recombinedCoordinate)
                }


                            print("Firebase alerts posts retrieved")

                print("                 MapArray.alertNotificationCoordinatesArray after  displayAlert snapshot is: \(MapArray.alertNotificationCoordinatesArray)")
                print("                     self.userAlertNotificationArray after  displayAlert snapshot is: \(self.userAlertNotificationArray)")
                self.mapView.addAnnotations(self.userAlertNotificationArray)
                setCompletion(true)

            })
        }
    }

【问题讨论】:

    标签: swift firebase firebase-realtime-database


    【解决方案1】:

    我终于明白我的错误在哪里了。我显然得到了父密钥。 在 snapshot.childrenfor in 循环中,我声明了一个变量来保存子快照值,然后像以前一样获取我的值。我只是没有意识到我必须输入从 Firebase 返回的字典作为快照才能读取条目。我希望这对其他初级程序员有所帮助,因为这让我很沮丧,但像往常一样,努力学习会带来很大的回报。

    所以现在完整的重写函数是:

    func displayAlerts(setCompletion: @escaping (Bool) -> ()) {
            print("                     MapArray.alertNotificationCoordinatesArray before displayAlert snapshot is: \(MapArray.alertNotificationCoordinatesArray)")
            print("                     self.userAlertNotificationArray before displayAlert snapshot is: \(self.userAlertNotificationArray)")
    
            if self.userAlertNotificationArray.count == 0 {
                ref = Database.database().reference()
    
                ref?.child("Continent").child("Europe").child("Country").child("Italy").child("Region").child("Emilia-Romagna").child("City").child("Bologna").child("Community").child("Alert Notifications").observeSingleEvent(of: .value, with: { (snapshot) in
                    print("         snapshot is: \(snapshot)")
                    guard let data = snapshot.value as? [String :[String:String]] else { return } // checking if the snapshot is the dictonary in the form of [String :[String:String]] else return
    
                    for snap in snapshot.children {
                        let alertSnap = snap as! DataSnapshot  // casting the snapshot children is the dictonary in the form of [String:String]
                        let childrenKey = alertSnap.key
                        let alert = alertSnap.value as! [String:String]
                        let dataLatitude = alert["Latitude"]!
                        let dataLongitude = alert["Longitude"]!
                        let type = alert["Description"]!
                        let id = Int(alert["Id"]!)!
                        print("Node Key is: \(snapshot.key)")
                        print("childrenKey is:\(childrenKey)")
                        print("dataLatitude is: \(String(describing: dataLatitude))")
                        print("dataLongitude is: \(String(describing: dataLongitude))")
                        print("type is: \(String(describing: type))")
                        print("id is: \(String(describing: id))")
    
                        print("data is: \(data)")
    
                        let doubledLatitude = Double(dataLatitude)
                        let doubledLongitude = Double(dataLongitude)
                        let recombinedCoordinate = CLLocationCoordinate2D(latitude: doubledLatitude!, longitude: doubledLongitude!)
    
                        let userAlertAnnotation = UserAlert(type: type, coordinate: recombinedCoordinate, firebaseKey: childrenKey, title: type,id: id)
                        self.userAlertNotificationArray.append(userAlertAnnotation)  // array of notifications coming from Firebase
                        MapArray.alertNotificationCoordinatesArray.append(recombinedCoordinate)
                    }
    
    
    
                    print("Firebase alerts posts retrieved")
    
                    print("                 MapArray.alertNotificationCoordinatesArray after  displayAlert snapshot is: \(MapArray.alertNotificationCoordinatesArray)")
                    print("                     self.userAlertNotificationArray after  displayAlert snapshot is: \(self.userAlertNotificationArray)")
                    self.mapView.addAnnotations(self.userAlertNotificationArray)
                    setCompletion(true)
    
                })
            }
        } 
    

    我不明白的一件事是投反对票,如果他们至少留下评论以帮助改进问题,那将是有用的,否则只会增加已经来寻求帮助的人的巨大挫败感对于他面临的一个问题。 希望这最后的评论也有帮助。这是一个很棒的社区,如果没有,我真的不会走到这一步。 能成为其中的一员,我感到非常自豪。 干杯。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-22
      • 1970-01-01
      • 2017-03-25
      • 1970-01-01
      • 2016-12-28
      • 1970-01-01
      • 1970-01-01
      • 2019-10-01
      相关资源
      最近更新 更多