2017-04-12 54 views
0

如何获取for case let模式匹配中迭代枚举元素的索引?用于案例模式匹配的枚举元素索引

我有一个枚举:

enum EnumType { 
    case A(associatedValue: Int) 
    case B(associatedValue: String) 
} 

和阵列枚举元素:

let arrayOfEnums: [EnumType] = [.A(1), B("A"), .A(2), B("B")] 

for环,其中I遍历仅A元件,我想获得当前迭代的元素的索引in arrayOfEnums array:

for case let EnumType.A(associatedValue) in arrayOfEnums { 
    // operations here 
} 

我kn自己,我可以从Array.enumerated()索引,但我不知道如何在for case做到这一点。

回答

0

我找到了解决办法:

for case let (index, EnumType.A(associatedValue)) in arrayOfEnums.enumerated() { 
    // operations here 
}