迭代Firebase阵列

时间:2017-09-18 00:19:26

标签: arrays json firebase swift3 firebase-realtime-database

当我获得数据库的快照时,我看到了:

{
    "9-17-2017" =     {
        "19:26:38" =         {
            1abA = 1ab;
            1abrA = 1;
            2abA = 2ab;
            2abrA = 2;
            3abA = 3ab;
            4abA = 4ab;
            5abA = 5a;
            6abA = 6ab;
            7abA = 7ab;
            Date = "9-17-2017";
            Time = "19:26:38";
            anA = an;
            apnA = apn;
            q1A = 1;
            q2A = 2;
            q3A = 3;
            q4A = 4;
            q5A = 5;
            q6A = 6;
        };
        "20:16:15" =         {
            1abA = 1qwe;
            1abrA = 3;
            2abA = "D.C.";
            2abrA = 7;
            3abA = wedded;
            4abA = wedge;
            5abA = qdwfeg;
            6abA = qwfegr;
            7abA = wedge;
            Date = "9-17-2017";
            Time = "20:16:15";
            anA = "we're";
            apnA = apn;
            q1A = 7;
            q2A = 2;
            q3A = 7;
            q4A = 4;
            q5A = 5;
            q6A = 7;
        };
    };
}

我希望能够将标签设置为键1abA的值,但我似乎无法找到对数组进行排序的方法。

我正在使用此代码来获取快照:

let userID = Auth.auth().currentUser?.uid
let ref = Database.database().reference().child("users").child(userID!) ref.child("PostGameEval").observe(.value, with: { (snapshot) in
            print(snapshot.value!)
        })

1 个答案:

答案 0 :(得分:1)

我试过希望这可以帮助你:

我已经采用如上图所示的数据:

此处dict相当于上面的snapshot.value

    let dict = [
        "9-17-2017" :    [
                "19:26:38" :        [
                        "1abA" : "1ab",
                        "1abrA" : 1
                ],
                "20:16:15" :         [
                        "1abA" : "1qwe",
                        "1abrA" : 3

                ],
                "19:23:38" :        [
                        "1abA" : "1ab",
                        "1abrA" : 1
                ],
                "21:16:15" :         [
                        "1abA" : "1qwe",
                        "1abrA" : 3

                ]

        ],
        "9-16-2017":   [
                "19:20:38" :        [
                        "1abA" : "1ax",
                        "1abrA" : 4
                ],
                "20:01:15" :         [
                        "1abA" : "1qe",
                        "1abrA" : 0

                ],
                "19:23:38" :        [
                        "1abA" : "1ab",
                        "1abrA" : 1
                ],
                "21:16:15" :         [
                        "1abA" : "1qwe",
                        "1abrA" : 3

                ]

        ]

]

注意:我认为在您的情况下,您可以按如下方式获取dict

if let dict = snaphot.value as? [String : [String: [String: Any]] {
    //then use the following code here
}

现在获取密钥1abA的值我使用了这段代码:

let keys = Array(dict.keys)

var myLabel = ""
for i in 0..<keys.count {
        if let innerDict = dict[keys[i]] {
                let innerKeys = Array((innerDict).keys)
                let sortedInnerKeys = innerKeys.sorted(by: { $0 > $1} )
                for j in 0..<2 {
                        if let tempDict = innerDict[sortedInnerKeys[j]] {
                                //myLabel = tempDict["1abA"] as! String
                                print("Value of Key 1abA for innerKey \(sortedInnerKeys[j]) is \(tempDict["1abA"] ?? "")")
                                print("Value of Key 1abrA for innerKey \(sortedInnerKeys[j]) is \(tempDict["1abrA"] ?? "")")
                        }
                }
        }

}

输出:

  

内键21:16:15的键1abA的值是1qwe

     

内键21:16:15的键1abrA值为3

     

内部密钥20:01:15的密钥1abA的值是1qe

     

内部密钥20:01:15的密钥1abrA的值为0

     

内键21:16:15的键1abA的值是1qwe

     

内键21:16:15的键1abrA值为3

     

内键20:16:15的键1abA的值是1qwe

     

内部密钥20:16:15的密钥1abrA值为3