Get Count of Swift Enum Values


In following enum example, I have added a static variable named count which calculates and returns count of the given enum.

enum ToDoPriority: Int {
  case High
  case Medium
  case Low
  var stringValue: String {
    switch self {
    case .High:
      return "High"
    case .Medium:
      return "Medium"
    case .Low:
      return "Low"
    }
  }
  static let count: Int = {
    var max: Int = 0
    while let _ = ToDoPriority(rawValue: max) { max += 1 }
    return max
  }()
}