Skip to content

Instantly share code, notes, and snippets.

@abbeyjackson
Created May 5, 2016 19:32
Show Gist options
  • Save abbeyjackson/65243bb0a59115694cefce412f8174dc to your computer and use it in GitHub Desktop.
Save abbeyjackson/65243bb0a59115694cefce412f8174dc to your computer and use it in GitHub Desktop.
iterate through an enum. Not possible but can fake it like this. If you conform to ForwardIndexType you can also use methods like advancedBy and distanceTo
enum Number: String, ForwardIndexType {
case One
case Two
case Three
case Four
case EndIndex
func successor() -> Number
{
switch self {
case .One:
return .Two
case .Two:
return .Three
case .Three:
return .Four
case .Four:
return .EndIndex
case .EndIndex:
return .EndIndex
}
}
}
var number = Number.One
number.advancedBy(2)
number.rawValue
var array: [String] = Array()
while number != Number.EndIndex {
array.append(number.rawValue)
number = number.successor()
}
print(array)
for item in array {
print(item)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment