我很困惑。
我可以使用以下方法循环使用类型转换的数组:
for case let provider as ProviderModel in providers {
provider.activated = true
}
但是我怎么能在这里实现索引Swift风格呢? 像这样:
for (index, provider <<type cast>>) in providers.enumerated() {
provider.activated = true
}
答案 0 :(得分:3)
使用元组模式:
for case (let offset, let provider as ProviderModel) in providers.enumerated() {
// ...
}
或等效地:
for case let (offset, provider as ProviderModel) in providers.enumerated() {
// ...
}
一个独立的例子:
let numbersAndStrings: [Any] = [1, "2", 3, "4", 5]
for case let (offset, number as Int) in numbersAndStrings.enumerated() {
print(number, "at offset", offset)
}
输出:
1 at offset 0 3 at offset 2 5 at offset 4