如何将Firebase快照转换为项目数组?

时间:2016-04-13 21:35:09

标签: ios swift firebase

{ 
    "body": "Hello there",
    "viewers": {
        "1": {
            "user_id": 1
        },
        "2":{
            "user_id": 2
        }
    }
}

以上是我从Firebase收到的快照。

        fireRef!.observeEventType(FEventType.Value) { [weak self] (snapshot: FDataSnapshot!) -> Void in
            if let stuff = snapshot.value as? [String:AnyObject] {
                print(stuff) //okay, it prints the snapshot correctly
                //Now, I need to access the "viewers" part, but how?
            }
        }

如何循环浏览每个查看器并打印用户ID?

1 个答案:

答案 0 :(得分:1)

用Firebase弹出我的上一个项目,我解决了这个问题如下:

if let viewers = stuff["viewers"] as? [String:[String:Int]] { //Firebase could deliver your numbers as strings, in that case try [String:[String:String]]
                for viewer in viewers {
                    let userID = viewer.1
                    print(userID.values.first)
                }
            }

可以采用更清洁的方式来实现它