Arrays 迭代Firebase数组
当我得到数据库的快照时,我看到:Arrays 迭代Firebase数组,arrays,json,firebase,swift3,firebase-realtime-database,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;
{
"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!)
})
我尝试了一下,希望这对您有所帮助: 我已取得上图所示数据如下:
if let dict = snaphot.value as? [String : [String: [String: Any]] {
//then use the following code here
}
此处的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
}
现在获取key1abA的值时,我使用了以下代码:
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"] ?? "")")
}
}
}
}
let keys=数组(dict.keys)
var myLabel=“”
因为我在0$1} )
对于0中的j..“将标签设置为键1abA的值”是什么意思@Siyavash所以我希望能够将键1abA的值设置为uilabel,那么我如何才能挑出每个键并获得值?我如何循环该值以获得我原始的所有键值json@WillMays对不起,我听不懂,请举例说明,如果我想对q1A和q2a执行相同的操作,你是什么意思?我是否使用相同的代码,只是为每个值添加tempDict[“q1A”]等等?@WillMays是的,我相信是的,你可以为字典中的任何键获取值。如果我只需要基于时间的最后7个键,那又如何?