gpt4 book ai didi

ios - 在 swift 3.1 中使用 Firebase SDK 对从 snapshot.value 获取的字典数组进行排序

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:23:43 26 4
gpt4 key购买 nike

我有一个包含数据的数组,因为在通过 firebase 获取数据时,有时数据会以正确的顺序出现,而有时则随机出现。因此,需要使用排序方法对我的本地数组进行排序

来自 snapshot.value 的响应:

    Optional({
Images = {
"-KmRCabAyzoVNMD06Hpr" = {
UploadTime = 1497269431993;
imageUrl = "https://firebasestorage.googleapis.com/v0/b/mychat-1147d.appspot.com/o/Images%2FwRHmnmfu3CeqXa77YgHEWVXI1AI3%2F518962226641.jpg?alt=media&token=645878ac-1124-44b0-8203-75e94813ca76";
};
"-KmRCnYSsjJaZ3RINRDG" = {
UploadTime = 1497269484338;
imageUrl = "https://firebasestorage.googleapis.com/v0/b/mychat-1147d.appspot.com/o/Images%2FwRHmnmfu3CeqXa77YgHEWVXI1AI3%2F518962275721.jpg?alt=media&token=d0113b12-531c-48a5-838b-02f96396a6df";
};
"-KmRCweslwMPBt0aCBIc" = {
UploadTime = 1497269521676;
imageUrl = "https://firebasestorage.googleapis.com/v0/b/mychat-1147d.appspot.com/o/Images%2FwRHmnmfu3CeqXa77YgHEWVXI1AI3%2F518962317708.jpg?alt=media&token=e931d9b3-5635-4e2f-a597-2c60aa25b38d";
};
"-KmRD13t3pI21ci45LEv" = {
UploadTime = 1497269544342;
imageUrl = "https://firebasestorage.googleapis.com/v0/b/mychat-1147d.appspot.com/o/Images%2FwRHmnmfu3CeqXa77YgHEWVXI1AI3%2F518962340349.jpg?alt=media&token=a738df76-e00b-48c2-9983-1e902e285a5f";
};
"-KmRD6WfaNTgKaVhy_Iq" = {
UploadTime = 1497269566141;
imageUrl = "https://firebasestorage.googleapis.com/v0/b/mychat-1147d.appspot.com/o/Images%2FwRHmnmfu3CeqXa77YgHEWVXI1AI3%2F518962359997.jpg?alt=media&token=5828ee44-1721-4267-99d7-91f10381aa13";
};
"-KmREsVwMb4sY6yN4pNK" = {
UploadTime = 1497270028928;
imageUrl = "https://firebasestorage.googleapis.com/v0/b/mychat-1147d.appspot.com/o/Images%2FwRHmnmfu3CeqXa77YgHEWVXI1AI3%2F518962824146.jpg?alt=media&token=aa0383c7-7d26-4b7a-b9bb-dbb1da3cdfff";
};
};
UserInfo = {
UserID = wRHmnmfu3CeqXa77YgHEWVXI1AI3;
userName = "Aric D'sooza";
};
})

我需要根据上传时间对这个数组进行排序。

我试过这个:

let sortedArray = (myArray ).sorted(by: { (dictOne, dictTwo) -> Bool in
let d1 = dictOne["UploadTime"]! as! Double
let d2 = dictTwo["UploadTime"]! as! Double

return d1 < d2
})
print("Sorted Array - %@",sortedArray)
}

也尝试过:

self.sortedArray = (myArray as NSArray).sortedArray(using: [NSSortDescriptor(key: "UploadTime", ascending: true)]) as! [[String:AnyObject]]

最佳答案

试试这段代码,它对我有用。

 func sortDictonary(aDataDict:[String:AnyObject]) ->[[String:Any]] {

let allKeys = Array(aDataDict.keys)
var arrImages = [[String:Any]] ()
for key in allKeys{
var aDict = aDataDict[key] as! [String:AnyObject]
let aTempData = ["uploadTime" :aDict["UploadTime"] as! Double,
"imageUrl":aDict["imageUrl"] as! String,
"AutoId": key] as [String : AnyObject]
// Here AutoID is unique key from your Response e.g. -KmREsVwMb4sY6yN4pNK
arrImages.append(aTempData)
}
return arrImages
}

调用此函数并将您的 Images Dictionary 作为 ,

var aDataDict = yourJSONResponse as! Dictionary<String,AnyObject>
var aDictUserInfo = aDataDict["UserInfo"] as!Dictionary<String,AnyObject>
let aDictImages = aDataDict["Images"] as! Dictionary<String,AnyObject>
let arrTempImages :[[String:Any]] = self.sortDictonary(aDataDict: aDictImages)

你会得到排序的数组,

arrImagesData = (arrImagesData as NSArray).sortedArray(using: [NSSortDescriptor(key: "uploadTime", ascending: true)]) as! [[String:AnyObject]]

关于ios - 在 swift 3.1 中使用 Firebase SDK 对从 snapshot.value 获取的字典数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44499979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com