使用swift检索iOS应用程序的firebase数据



我正在使用firebase来收集数据,我正在尝试以iPhone应用程序可用的格式检索数据,但我无法正确地获取数据。我正在用Swift编写应用程序。

数据按日期字符串分组,然后是带有随机键的,然后是数据。例如:

{
  "20160304" : {
    "-KC-aOwSWpt4dlYmjJE4" : {
      "coordinates" : "-37.7811465912404, 145.005993055861",
      "event" : "Test event",
      "time" : "2016-03-04 07:48:43 +0000"
    }, etc...

到目前为止,我正在获取这样的数据:

ref.queryOrderedByKey().observeEventType(.ChildAdded, withBlock: {
        snapshot in
        //print(snapshot.key) // date
        print(snapshot.value)
    })

它向控制台返回这样的内容:

{
"-KD8O0gL7gDGu_hRyFzQ" =     {
    coordinates = "-37.7540958861003, 145.001224694195";
    event = "Test event";
    time = "2016-03-18 11:02:32 +0000";
}; etc...

有人知道我如何进入下一个层次,通过随机键,获得有意义的数据吗?我以前在javascript中遇到过这个问题,但使用swift让我很困惑。我希望能够获取定义日期(顶层)的详细数据(底层)。

试试这个代码

let jsonLocations = snapshot.valueInExportFormat() as! NSDictionary
let keys = jsonLocations.allKeys
for key in keys {
    let json = jsonLocations[key] as! [String: AnyObject]
    self.sections.append(Location(JSONObject: json))
}

我通常尽可能长时间地坚持FDatasnapshot的方法,这导致:

ref.queryOrderedByKey().observeEventType(.ChildAdded, withBlock: { snapshot in
    for child in snapshot.children {
        print(child.key); // -KC-aOwSWpt4dlYmjJE4
        print(child.childSnapshotForPath("event").value)
    }
});

最新更新