Skip to content

Instantly share code, notes, and snippets.

@BoshiLee
Last active March 9, 2017 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BoshiLee/442fe35c544c8ce636b1e6991b76e1c5 to your computer and use it in GitHub Desktop.
Save BoshiLee/442fe35c544c8ce636b1e6991b76e1c5 to your computer and use it in GitHub Desktop.
enum BookType: String {
case computer
case science
case language
static let cases: [BookType] = [.computer, .science, .language]
init?(_ ix: Int) {
guard ix < BookType.cases.count else {
return nil
}
self = BookType.cases[ix]
}
// init without saying rawvalue
init?(_ rawValue : String) {
self.init(rawValue: rawValue)
}
func nextCase() -> BookType {
var ix = BookType.cases.index(of: self)
ix = (ix! + 1) % BookType.cases.count
return BookType.cases[ix!]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment