swift 3 如何将数组中的数字按顺序放入字典中?
How to put array numbers into a dictionary in sequence in swift 3?
我在这里尝试了以下代码,但没有得到所需的输出我需要将数组显示为字典以获得所需的输出,因为我拥有的 ["0.0": "50.0", "50.0": "100.0", "100.0": "150.0"]
是 [0.0, 50.0, 100.0, 150.0]
in self.filteredPriceArr
谁能帮我按要求实现这个?
for (index,item) in self.filteredPriceArr.enumerated() {
print(index)
print(self.filteredPriceArr.count)
if index + 1 != priceArray.count {
priceDict.updateValue("\(self.filteredPriceArr[index + 1])", forKey: "\(self.filteredPriceArr[index])")
}
print(priceDict)
}
我把你的代码放了,只有一个错误
也就是priceArray
应该是filteredPriceArr
var filteredPriceArr = [0.0, 50.0, 100.0, 150.0]
var priceDict:[String:String] = [:]
for (index,item) in filteredPriceArr.enumerated() {
print(index)
print(filteredPriceArr.count)
if index + 1 != filteredPriceArr.count {
priceDict.updateValue("\(filteredPriceArr[index + 1])", forKey: "\(filteredPriceArr[index])")
}
}
print(priceDict)
这里是输出
["0.0": "50.0", "100.0": "150.0", "50.0": "100.0"]
按照你问题中提到的顺序,你无法得到它
["0.0": "50.0", "50.0": "100.0", "100.0": "150.0"]
字典无法排序!!
我在这里尝试了以下代码,但没有得到所需的输出我需要将数组显示为字典以获得所需的输出,因为我拥有的 ["0.0": "50.0", "50.0": "100.0", "100.0": "150.0"]
是 [0.0, 50.0, 100.0, 150.0]
in self.filteredPriceArr
谁能帮我按要求实现这个?
for (index,item) in self.filteredPriceArr.enumerated() {
print(index)
print(self.filteredPriceArr.count)
if index + 1 != priceArray.count {
priceDict.updateValue("\(self.filteredPriceArr[index + 1])", forKey: "\(self.filteredPriceArr[index])")
}
print(priceDict)
}
我把你的代码放了,只有一个错误
也就是priceArray
应该是filteredPriceArr
var filteredPriceArr = [0.0, 50.0, 100.0, 150.0]
var priceDict:[String:String] = [:]
for (index,item) in filteredPriceArr.enumerated() {
print(index)
print(filteredPriceArr.count)
if index + 1 != filteredPriceArr.count {
priceDict.updateValue("\(filteredPriceArr[index + 1])", forKey: "\(filteredPriceArr[index])")
}
}
print(priceDict)
这里是输出
["0.0": "50.0", "100.0": "150.0", "50.0": "100.0"]
按照你问题中提到的顺序,你无法得到它
["0.0": "50.0", "50.0": "100.0", "100.0": "150.0"]
字典无法排序!!