Created
March 14, 2020 19:42
-
-
Save xinmyname/7112e80e2042cc2d9c4b859b7595c7db to your computer and use it in GitHub Desktop.
Enums in Swift
This file contains 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
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