Last active
April 11, 2016 14:06
-
-
Save a40yostudent/168fd2b0597d45390e0beedfb9de80dc to your computer and use it in GitHub Desktop.
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
/* | |
Convenient way to enumerate an enum, see also: | |
http://www.swift-studies.com/blog/2014/6/10/enumerating-enums-in-swift | |
http://www.wooji-juice.com/blog/stupid-swift-tricks-5-enums.html | |
https://gist.github.com/erica/b849cef7fcd123756373 | |
Sabino Paulicelli | |
*/ | |
protocol SmartEnum { | |
static var allValues: [Self] { get } | |
} | |
extension SmartEnum where Self: RawRepresentable, Self.RawValue == Int { | |
static var allValues: [Self] { | |
var result: [Self] = [] | |
var value = 1 | |
while let item = Self(rawValue: value) { | |
result.append(item) | |
value += 1 | |
} | |
return result | |
} | |
} | |
enum Rank: Int, CustomStringConvertible, SmartEnum { | |
case Ace = 1 | |
case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten | |
case Jack, Queen, King | |
var description: String { | |
return ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"][rawValue-1] | |
} | |
} | |
for i in Rank.allValues { | |
print(i) | |
} | |
for (i, j) in Rank.allValues.enumerate() { | |
print(i, j) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment