Firebase在Swift中迭代快照

时间:2016-02-23 15:59:21

标签: swift firebase

我在我的应用中有这个代码:

DataService.dataService.USERS_REF.childByAppendingPath(DataService.dataService.CURRENT_USER_ID).childByAppendingPath("groups").observeSingleEventOfType(.Value, withBlock: { snapshot in

  print(snapshot)

})

如果我打印结果,我只会得到一个带有此值的快照:

Snap (groups) {
    "-KBAX-d4UNQMZErrTqPD" = true;
    "-KBAX0rrrR-trRZc408F" = true;
    "-KBAX2-lrGd-Bx2zVizv" = true;
    "-KBCL_xUjm-kf1JsGgpA" = true;
    "-KBCOlullSmzi46ecf9J" = true;
}

如果我打印snapshot.key,我会

groups

如果我打印snapshot.value,我会得到

{
    "-KBAX-d4UNQMZErrTqPD" = true;
    "-KBAX0rrrR-trRZc408F" = true;
    "-KBAX2-lrGd-Bx2zVizv" = true;
    "-KBCL_xUjm-kf1JsGgpA" = true;
    "-KBCOlullSmzi46ecf9J" = true;
}

有没有办法迭代所有快照值并获取每个快照值的所有键和值?在这种情况下,此快照中有5个键和值。

1 个答案:

答案 0 :(得分:2)

没关系,我已经找到了解决方案。在这种情况下:

DataService.dataService.USERS_REF.childByAppendingPath(DataService.dataService.CURRENT_USER_ID).childByAppendingPath("groups").observeSingleEventOfType(.Value, withBlock: { snapshot in

    for rest in snapshot.children.allObjects as! [FDataSnapshot] {
        print(rest.key)
    }
})