我有以下对象:
(评分)
{
EUR: "1"
RON: 4.5
USD: 1.3
.
.
.
n: INT/STRING
}
有没有这样做的功能?
答案 0 :(得分:1)
如果您关心索引,可以使用'传统'for循环 - 虽然@Eric指出这很快就会完全删除
for var i = 0; i < rates.count; i++
{
let rate = rates[i]
// do stuff with rate
}
枚举方法如下所示
for (index, rate) in rates.enumerate()
{
print("Do stuff with \(rate) at position \(index)")
}
如果您只是依次需要每个对象,那就更容易了
for rate in rates
{
// do stuff with rate
}
答案 1 :(得分:0)
因为它是字典,所以不需要enumerate():
var aDictionary: [String: Float] = ["EUR": 1, "RON": 4.5,
"USD": 1.3]
for (index,item) in aDictionary{
print(index,item)
}