Skip to content

Instantly share code, notes, and snippets.

@xinmyname
Created March 14, 2020 19:42
Show Gist options
  • Save xinmyname/7112e80e2042cc2d9c4b859b7595c7db to your computer and use it in GitHub Desktop.
Save xinmyname/7112e80e2042cc2d9c4b859b7595c7db to your computer and use it in GitHub Desktop.
Enums in Swift
public enum Kind:Int,CustomStringConvertible {
case weapon
case armor
case scroll
case wand
case potion
case ring
public var description:String {
switch self {
case .weapon: return "weapon"
case .armor: return "armor"
case .scroll: return "scroll"
case .wand: return "wand"
case .potion: return "potion"
case .ring: return "ring"
}
}
private static var _count:Int {
get {
var max:Int = 0
while let _ = Kind(rawValue: max) {
max += 1
}
return max
}
}
static func oneAtRandom() -> Kind {
return Kind(rawValue: Int(arc4random_uniform(UInt32(_count))))!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment