Created
June 11, 2018 08:49
-
-
Save jimmythai/09934564aa43e87d6fe644728849afec to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // If rawValue is `Int`... | |
| enum FizzBuzz: Int { | |
| case fizz | |
| case buzz | |
| static let count: Int = { | |
| var count = 0 | |
| while let _ = FizzBuzz(rawValue: max) { count += 1 } | |
| return count | |
| }() | |
| } | |
| // Extension | |
| protocol EnumCountable { | |
| static var count: Int { get } | |
| } | |
| extension EnumCountable where Self: RawRepresentable, Self.RawValue == Int { | |
| static var count: Int { | |
| var count = 0 | |
| while let _ = Self(rawValue: count) { count += 1 } | |
| return count | |
| } | |
| } | |
| // If rawValue is not `Int`... | |
| enum FizzBuzz: String { | |
| case fizz | |
| case buzz | |
| case count | |
| } | |
| FizzBuzz.count.hashValue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment