Created
May 4, 2019 13:47
-
-
Save Sajjon/e7e8b6ca5a2281d3bf2daf1f86883d9f to your computer and use it in GitHub Desktop.
Season from Month
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
| enum Month: Int, CaseIterable { | |
| case jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec | |
| } | |
| extension Month { | |
| var season: Season { | |
| return Season(month: self) | |
| } | |
| } | |
| enum Season: Int, CaseIterable { | |
| case winter, spring, summer, autumn | |
| } | |
| extension Season { | |
| init(month: Month) { | |
| let seasonIndex = ((month.rawValue + 1 ) / (Month.count / Season.count)) % Season.count | |
| guard let season = Season(rawValue: seasonIndex) else { | |
| fatalError("Incorrect implementation of `seasonIndex` (was: \(seasonIndex)) and/or rawValue of 'Season'") | |
| } | |
| self = season | |
| } | |
| } | |
| extension CaseIterable { | |
| static var count: Int { return Self.allCases.count } | |
| } | |
| for month in Month.allCases { | |
| print("\(month) is \(month.season)") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment