Is it possible to programatically find out how many "cases" an Enum has in Swift 2 and iterate over them?
This code doesn't compile, but it gives yo an idea of what I'm trying to achieve:
enum HeaderStyles{
case h1
case h2
case h3
}
for item in HeaderStyles{
print(item)
}
The easiest way to iterate over all cases is to make a computed property which returns an Array
of them:
enum SomeEnum {
case Zero, One, Two
static var allCases: [SomeEnum] {
return [.Zero, .One, .Two]
}
}
If you want an automatic array you could use Int
as rawValue so you don't have to change any code if you add an additional case
:
Swift 3/4: ++
and --
were removed and anyGenerator
was renamed to AnyIterator
enum SomeEnum: Int {
case Zero, One, Two
static var allCases: [SomeEnum] {
var i = 0
return Array(AnyIterator{
let newEnum = SomeEnum(rawValue: i)
i += 1
return newEnum
})
}
}
Swift 2
enum SomeEnum: Int {
case Zero, One, Two
static var allCases: [SomeEnum] {
var i = 0
return Array(anyGenerator{ SomeEnum(rawValue: i++) })
}
}
In both cases you would use it like this:
for num in SomeEnum.allCases {
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With