1

Given the code below, is it possible in Swift to programmatically determine the number of values inside an enum? The goal is to avoid hard coding a number in order to randomly choose a type of this enum.

enum Direction: Int {
    case North
    case East
    case South
    case West
    case Northeast
    case Southeast
    case Southwest
    case Northwest

    static func random() -> Direction {
        return Direction(rawValue: Int(arc4random_uniform(8)) + 1)!
    }
}
Crashalot
  • 33,605
  • 61
  • 269
  • 439

0 Answers0