2017-04-12 56 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 
}