Ios 将Firebase字典数据转换为数组(Swift)

Ios 将Firebase字典数据转换为数组(Swift),ios,arrays,swift,dictionary,firebase,Ios,Arrays,Swift,Dictionary,Firebase,这可能是一个简单的答案,所以请提前道歉,但我被卡住了,因为我仍然对Firebase的工作原理感到困惑。我想根据保存在那里的unix日期数据查询Firebase数据库,然后获取相关的唯一ID数据并将其放入数组中 Firebase中的数据如下所示: posts node_0 Unix Date: Int Unique ID Event Number: Int node_1 Unix Date: Int Unique ID Event Number: Int

这可能是一个简单的答案,所以请提前道歉,但我被卡住了,因为我仍然对Firebase的工作原理感到困惑。我想根据保存在那里的unix日期数据查询Firebase数据库,然后获取相关的唯一ID数据并将其放入数组中

Firebase中的数据如下所示:

posts
  node_0
    Unix Date: Int
    Unique ID Event Number: Int
  node_1
    Unix Date: Int
    Unique ID Event Number: Int
  node_2
    Unix Date: Int
    Unique ID Event Number: Int
到目前为止,我掌握的情况如下。查询部分似乎按预期工作。我所面临的困难是如何将唯一ID事件号数据放入数组中。这似乎是最接近成功的方法,基于此,但我得到一个错误,即child没有任何有价值的成员

// Log user in
if let user = FIRAuth.auth()?.currentUser {

    // values for vars sevenDaysAgo and oneDayAgo set here

    ...

    let uid = user.uid

    //Query Database to get the places searched by the user between one and seven days ago.
    let historyRef = self.ref.child("historyForFeedbackLoop/\(uid)")

    historyRef.queryOrdered(byChild: "Unix Date").queryStarting(atValue: sevenDaysAgo).queryEnding(atValue: oneDayAgo).observeSingleEvent(of: .value, with: { snapshot in

        if (snapshot.value is NSNull) {
            print("error")
        } else {
            for child in snapshot.children {
                if let uniqueID = child.value["Unique ID Event Number"] as? Int {
                    arrayOfUserSearchHistoryIDs.append(uniqueID)
                }
            }
        }
    })

} else {
    print("auth error")
}
任何想法都非常感谢

尝试使用以下选项:-

   historyRef.queryOrdered(byChild: "Unix Date").queryStarting(atValue: sevenDaysAgo).queryEnding(atValue: oneDayAgo).observeSingleEvent(of: .value, with: { snapshot in

       if let snapDict = snapshot.value as? [String:AnyObject]{

               for each in snapDict{

                       let unID = each.value["Unique ID Event Number"] as! Int   
                       arrayOfUserSearchHistoryIDs.append(unID)  
                    }

              }else{
               print("SnapDict is null")
          }

        })
试着用这个:-

   historyRef.queryOrdered(byChild: "Unix Date").queryStarting(atValue: sevenDaysAgo).queryEnding(atValue: oneDayAgo).observeSingleEvent(of: .value, with: { snapshot in

       if let snapDict = snapshot.value as? [String:AnyObject]{

               for each in snapDict{

                       let unID = each.value["Unique ID Event Number"] as! Int   
                       arrayOfUserSearchHistoryIDs.append(unID)  
                    }

              }else{
               print("SnapDict is null")
          }

        })

最后,我重新研究了如何根据中概述的方法读取Firebase数据。我使用的实际工作代码如下,以防对其他人有所帮助

// Log user in
if let user = FIRAuth.auth()?.currentUser {

   let uid = user.uid
   // values for vars sevenDaysAgo and oneDayAgo set here

   ...

   let historyRef = self.ref.child("historyForFeedbackLoop/\(uid)")
    historyRef.queryOrdered(byChild: "Unix Date").queryStarting(atValue: sevenDaysAgo).queryEnding(atValue: oneDayAgo).observeSingleEvent(of: .value, with: { snapshot in

        if (snapshot.value is NSNull) {
            print("user data not found")
        }
        else {

             for child in snapshot.children {

                let data = child as! FIRDataSnapshot
                let value = data.value! as! [String:Any]
                self.arrayOfUserSearchHistoryIDs.append(value["Unique ID Event Number"] as! Int)
             }
        }
   })
}

最后,我重新研究了如何根据中概述的方法读取Firebase数据。我使用的实际工作代码如下,以防对其他人有所帮助

// Log user in
if let user = FIRAuth.auth()?.currentUser {

   let uid = user.uid
   // values for vars sevenDaysAgo and oneDayAgo set here

   ...

   let historyRef = self.ref.child("historyForFeedbackLoop/\(uid)")
    historyRef.queryOrdered(byChild: "Unix Date").queryStarting(atValue: sevenDaysAgo).queryEnding(atValue: oneDayAgo).observeSingleEvent(of: .value, with: { snapshot in

        if (snapshot.value is NSNull) {
            print("user data not found")
        }
        else {

             for child in snapshot.children {

                let data = child as! FIRDataSnapshot
                let value = data.value! as! [String:Any]
                self.arrayOfUserSearchHistoryIDs.append(value["Unique ID Event Number"] as! Int)
             }
        }
   })
}

宾果,邦戈,邦戈。这是快照。值不是子对象,duh。谢谢事实上,我说得太快了。它不起作用,因为我得到了一个错误:let unID=行的下标使用不明确。我试着运用这篇文章中的想法,但没有成功。有什么想法吗?您是用swift 2还是swift 3编码?对于swift 2,请使用each.1[唯一ID事件编号],我建议不要在数据库名称中使用空格,否则会导致错误。将其更改为唯一\u ID\u事件\u编号WIFT 3。关于空间的观点很好。我将不再这样做。你认为这是造成错误的原因吗?或者你只是一般性地评论一下?只是一般性的…你是在编译之后还是在viewController本身之前得到错误。宾果,邦戈,邦戈。这是快照。值不是子对象,duh。谢谢事实上,我说得太快了。它不起作用,因为我得到了一个错误:let unID=行的下标使用不明确。我试着运用这篇文章中的想法,但没有成功。有什么想法吗?您是用swift 2还是swift 3编码?对于swift 2,请使用each.1[唯一ID事件编号],我建议不要在数据库名称中使用空格,否则会导致错误。将其更改为唯一\u ID\u事件\u编号WIFT 3。关于空间的观点很好。我将不再这样做。您认为这是导致错误的原因吗?还是您只是对其进行了一般性的评论?只是一般性的…您是在编译之后还是在viewController本身之前收到错误。