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
  • 32,144
  • 59
  • 256
  • 415

0 Answers0