Created
May 5, 2016 19:32
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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