如何从 Swift 中的元组数组中获取键
How to get keys from Array of tuple in Swift
我正在从 API 中获取字典 [String: Int],并按值对其进行排序。在那里,我得到了一个元组数组,在那个元组数组中,我需要分隔键,并希望以相同的排序顺序使用这些键创建一个数组。
我尝试了一种从该数组中获取对象并将其添加到字典中的方法,但效果不佳,因为在添加键时或添加键后字典变得无序,需要帮助来解决此问题
尝试过的代码
let personalInfoDict = screenConfigResponse?.screenConfiguration?.personalInformation
let personalDict : [String : Int]?
if let dict = personalInfoDict, dict.count > 0 {
personalDict = self.sortWithKeys(dict)
}
func sortWithKeys(_ dict: [String: Int]) -> [String: Int] {
let sorted = dict.sorted(by: { [=11=].value < .value })
var newDict: [String: Int] = [:]
for sortedDict in sorted {
newDict[sortedDict.key] = sortedDict.value
}
return newDict
}
进行排序时,我得到了正确排序的顺序,但是当我循环并将它添加到 newDict 中时,它变得无序了,有没有办法只从元组数组中获取键
也许你可以将它转换为元组数组:
let personalInfoDict = screenConfigResponse?.screenConfiguration?.personalInformation
let personalPairs = personalInfoDict
.reduce(into: [(String, Int)]()) { [=10=].append((.key, .value)) }
.sorted(by: { [=10=].0 < .0 })
if let pArray = personalPairs, pArray.count > 0 {
for obj in pArray {
personalInformationArray.append(obj.0)
}
}
现在你有一个 [(String, Int)]
有序数组
对于文学词典,有一种哈希表。
但是 HashTables 不是有序的 数据结构,所以你得到的效果是预期的行为。
但是,Apple 提供了一个可选包,可以将其他数据结构添加到您的项目中 (https://github.com/apple/swift-collections)。特别是我认为“OrderedDictionary”是您的解决方案。
let baseDict : [String:Int] = ["Test1" : 5,
"Test2" : 1,
"Test3" : 4]
let orderedDict : OrderedDictionary<String, Int> = sortWithKeys(baseDict: baseDict)
print(orderedDict)
func sortWithKeys(baseDict : [String:Int]) -> OrderedDictionary<String, Int>
{
let temp = baseDict.sorted(by: { [=10=].value < .value })
var newDict : OrderedDictionary<String, Int> = [:]
for element in temp {
newDict[element.key] = element.value
}
return newDict
}
我正在从 API 中获取字典 [String: Int],并按值对其进行排序。在那里,我得到了一个元组数组,在那个元组数组中,我需要分隔键,并希望以相同的排序顺序使用这些键创建一个数组。
我尝试了一种从该数组中获取对象并将其添加到字典中的方法,但效果不佳,因为在添加键时或添加键后字典变得无序,需要帮助来解决此问题
尝试过的代码
let personalInfoDict = screenConfigResponse?.screenConfiguration?.personalInformation
let personalDict : [String : Int]?
if let dict = personalInfoDict, dict.count > 0 {
personalDict = self.sortWithKeys(dict)
}
func sortWithKeys(_ dict: [String: Int]) -> [String: Int] {
let sorted = dict.sorted(by: { [=11=].value < .value })
var newDict: [String: Int] = [:]
for sortedDict in sorted {
newDict[sortedDict.key] = sortedDict.value
}
return newDict
}
进行排序时,我得到了正确排序的顺序,但是当我循环并将它添加到 newDict 中时,它变得无序了,有没有办法只从元组数组中获取键
也许你可以将它转换为元组数组:
let personalInfoDict = screenConfigResponse?.screenConfiguration?.personalInformation
let personalPairs = personalInfoDict
.reduce(into: [(String, Int)]()) { [=10=].append((.key, .value)) }
.sorted(by: { [=10=].0 < .0 })
if let pArray = personalPairs, pArray.count > 0 {
for obj in pArray {
personalInformationArray.append(obj.0)
}
}
现在你有一个 [(String, Int)]
有序数组
对于文学词典,有一种哈希表。 但是 HashTables 不是有序的 数据结构,所以你得到的效果是预期的行为。
但是,Apple 提供了一个可选包,可以将其他数据结构添加到您的项目中 (https://github.com/apple/swift-collections)。特别是我认为“OrderedDictionary”是您的解决方案。
let baseDict : [String:Int] = ["Test1" : 5,
"Test2" : 1,
"Test3" : 4]
let orderedDict : OrderedDictionary<String, Int> = sortWithKeys(baseDict: baseDict)
print(orderedDict)
func sortWithKeys(baseDict : [String:Int]) -> OrderedDictionary<String, Int>
{
let temp = baseDict.sorted(by: { [=10=].value < .value })
var newDict : OrderedDictionary<String, Int> = [:]
for element in temp {
newDict[element.key] = element.value
}
return newDict
}