Skip to content

Instantly share code, notes, and snippets.

@donly
Created March 9, 2020 07:51
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 donly/86db95d37b36bdd647841b417c86fedf to your computer and use it in GitHub Desktop.
Save donly/86db95d37b36bdd647841b417c86fedf to your computer and use it in GitHub Desktop.
After implementing the description property and declaring CustomStringConvertible conformance, the Enum type provides its own custom representation.
enum Rank: Int, CustomStringConvertible {
case ace = 1
case two, three, four, five, six, seven, eight, nine, ten
case jack, queen, king
func simpleDescription() -> String {
switch self {
case .ace:
return "ace"
case .jack:
return "jack"
case .queen:
return "queen"
case .king:
return "king"
default:
return String(self.rawValue)
}
}
var description: String {
return "test"
}
}
let ace = Rank.ace // test
let aceRawValue = ace.rawValue // 1
let ranks: [Rank] = [.ace, .jack, .king] // [test, test, test]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment