我正在尝试从Firebase中提取排行榜数据。当我查看"快照"我能够以正确的顺序获取数据。数据。一旦我使用var json = JSON(snapshot.value)到达该行,数据将失去其顺序。
我正在尝试将数据解析为swift obects,以便它们可以在我的swift应用程序中作为有序排行榜使用/显示。我可以找到一种解决方法,让我的数据很容易出错,但它困扰我。
func getScoresData() {
let scoresRef = ref.childByAppendingPath("/High Scores")
scoresRef.queryLimitedToFirst(10).observeSingleEventOfType(.Value, withBlock: { snapshot in
print(snapshot)
var json = JSON(snapshot.value)
print(json)
let theKeys = json.dictionary!.keys
for key in theKeys {
self.timeStampArray.append(key)
let tempName = json[key]["Name"]
self.namesArray.append(String(tempName))
let tempScore = json[key]["Score"]
self.scoresArray.append(String(tempScore))
}
})
}
这是我从打印(快照)中正确排序的输出:
Snap (High Scores) {
"-KC3MktE1jS5z9-tTEgJ" = {
".priority" = "-9";
Date = "1457140993.669797";
Name = brendan;
Score = 9;
UUID = "BD561F1C-72A8-4A01-A71A-850682E49D61";
};
"-KC3MktM-rWmoitem8na" = {
".priority" = "-9";
Date = "1457140993.677347";
Name = brendan;
Score = 9;
UUID = "BD561F1C-72A8-4A01-A71A-850682E49D61";
};
"-KC3MktW0iJfkAKjlCy4" = {
".priority" = "-9";
Date = "1457140993.686348";
Name = brendan;
Score = 9;
UUID = "BD561F1C-72A8-4A01-A71A-850682E49D61";
};
"-KC3MktapH6Mebw2sAUe" = {
".priority" = "-10";
Date = "1457140993.692581";
Name = brendan;
Score = 10;
UUID = "BD561F1C-72A8-4A01-A71A-850682E49D61";
};
"-KC3MktbOyltGaJZ_XtD" = {
".priority" = "-10";
Date = "1457140993.694163";
Name = brendan;
Score = 10;
UUID = "BD561F1C-72A8-4A01-A71A-850682E49D61";
};
"-KC3MktjFj6VVAJfitZp" = {
".priority" = "-10";
Date = "1457140993.701368";
Name = brendan;
Score = 10;
UUID = "BD561F1C-72A8-4A01-A71A-850682E49D61";
};
"-KC3Mktk0_f6_eM8wP1x" = {
".priority" = "-10";
Date = "1457140993.702492";
Name = brendan;
Score = 10;
UUID = "BD561F1C-72A8-4A01-A71A-850682E49D61";
};
"-KC3MktrZaStt9b5pID_" = {
".priority" = "-10";
Date = "1457140993.709843";
Name = brendan;
Score = 10;
UUID = "BD561F1C-72A8-4A01-A71A-850682E49D61";
};
"-KC3QALPl38gbDy4xLE2" = {
".priority" = "-10";
Date = "1457141888.477473";
Name = brendan;
Score = 10;
UUID = "BD561F1C-72A8-4A01-A71A-850682E49D61";
};
"-KC3qGAqZiJ9Vs40MwG7" = {
".priority" = "-10";
Date = "1457148990.24583";
Name = brendan;
Score = 10;
UUID = "BD561F1C-72A8-4A01-A71A-850682E49D61";
};
}
这是来自print(json)行的不正确排序的数据:
{
"-KC3MktrZaStt9b5pID_" : {
"Score" : "10",
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457140993.709843,
"Name" : "brendan"
},
"-KC3QALPl38gbDy4xLE2" : {
"Score" : 10,
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457141888.477473,
"Name" : "brendan"
},
"-KC3MktW0iJfkAKjlCy4" : {
"Score" : "9",
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457140993.686348,
"Name" : "brendan"
},
"-KC3MktbOyltGaJZ_XtD" : {
"Score" : "10",
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457140993.694163,
"Name" : "brendan"
},
"-KC3Mktk0_f6_eM8wP1x" : {
"Score" : "10",
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457140993.702492,
"Name" : "brendan"
},
"-KC3MktM-rWmoitem8na" : {
"Score" : "9",
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457140993.677347,
"Name" : "brendan"
},
"-KC3MktapH6Mebw2sAUe" : {
"Score" : "10",
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457140993.692581,
"Name" : "brendan"
},
"-KC3qGAqZiJ9Vs40MwG7" : {
"Score" : 10,
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457148990.24583,
"Name" : "brendan"
},
"-KC3MktjFj6VVAJfitZp" : {
"Score" : "10",
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457140993.701368,
"Name" : "brendan"
},
"-KC3MktE1jS5z9-tTEgJ" : {
"Score" : "9",
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457140993.669797,
"Name" : "brendan"
}
}
由于我使用json对象获取密钥,从而填充数组,因此数组中的数据乱序。
我错过了什么?
答案 0 :(得分:-1)
如果要维护排序,请使用数组而不是字典,无论是JSON
还是Swift对象。因此,为了维护排序,您可以从此更改JSON
结构:
{
"-KC3MktrZaStt9b5pID_" : {
"Score" : "10",
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457140993.709843,
"Name" : "brendan"
},
"-KC3QALPl38gbDy4xLE2" : {
"Score" : 10,
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457141888.477473,
"Name" : "brendan"
}
}
到此:
[
{
"Key": "-KC3MktrZaStt9b5pID_",
"Score": "10",
"UUID": "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date": 1457140993.709843,
"Name": "brendan"
},
{
"Key": "-KC3QALPl38gbDy4xLE2",
"Score": 10,
"UUID": "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date": 1457141888.477473,
"Name": "brendan"
}
]