Swift enums should be iterable using for-in loops
| Originator: | tim.sheridan | ||
| Number: | rdar://17174768 | Date Originated: | 20140605 |
| Status: | Duplicate/17102392 | Resolved: | 20140611 |
| Product: | Swift | Product Version: | |
| Classification: | New feature | Reproducible: | Always |
One of my most common things that I do with enums is to iterate through each possible member of the enum. GuidedTour.playground even makes you do such a task to generate a deck of cards.
But, Swift doesn't have a good support to do this. The following is rejected as an error ('Suit.Type' does not conform to protocol 'Sequence'), but I propose that it should work for this purpose:
enum Suit {
case Spades, Hearts, Diamonds, Clubs
}
for suit in Suit {
if suit == Suit.Spades {
println("ding")
}
}
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!
duplicated in 17262589
well said
In Haskell there is an Enumerable typeclass that can be derived for this purpose. Seems like a perfect use case for a Protocol, although it doesn't appear that Swift has such a built-in protocol or any way to automatically derive any Protocol implementation.