gpt4 book ai didi

ios - 根据对象属性过滤任何对象的数组

转载 作者:行者123 更新时间:2023-11-30 11:03:56 25 4
gpt4 key购买 nike

我收到了这种格式的回复

[
"1234": {
"startDate": "1536278400",
"endDate": "1536796800",
"playerRank": 1,
"performance": 100,
"isProfit": false,
"members": 1
},
"adityaKumar": {
"startDate": "1536364800",
"endDate": "1540080000",
"playerRank": 1,
"performance": 100,
"isProfit": false,
"members": 6
},
"madhu60": {
"startDate": "1539388800",
"endDate": "1539475200",
"playerRank": 1,
"performance": 100,
"isProfit": false,
"members": 2
}
]

此响应的类型为[string:Any]。我必须使用 playerRank 属性对数据进行排序。我希望输出的类型为 [string:Any]

这在 Swift 中怎么可能?

最佳答案

字典未排序

来自swift documentation

A dictionary stores associations between keys of the same type and values of the same type in an collection with no defined ordering.

但是,您可以在字典上使用 .sorted 方法,这将为您提供一个字典数组(数组是可排序的)。

let sorted = dictionary.sorted(by: { ($0.value["playerRank"]) < ($1.value["playerRank"]) })

从你原来的例子中sorted可能看起来像这样

[
(
"key": "madhu60",
"value": {
"startDate": "1539388800",
"endDate": "1539475200",
"playerRank": 1,
"performance": 100,
"isProfit": false,
"members": 2
}
)
....
]

关于ios - 根据对象属性过滤任何对象的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52968655/

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