Extend enums to have count
3lvis opened this issue · comments
Elvis Nunez commented
extension RawRepresentable where RawValue: IntegerType {
static var count: Int {
var i: RawValue = 0
while let _ = Self(rawValue: i) {
i = i.successor()
}
return Int(i.toIntMax())
}
}
Elvis Nunez commented
"Hey, but this doesn't work with String types!"
Doesn't matter, you shouldn't be using that anyway since it doesn't provide you a way to return localized variations. So maybe this should be better.
enum Section: Int {
case title, price, others, delete
func toString() -> String? {
switch self {
case .title:
return NSLocalizedString("Title", comment: "")
case .price:
return NSLocalizedString("Price", comment: "")
case .others:
return NSLocalizedString("Others", comment: "")
case .delete:
return nil
}
}
}
Igor Ranieri commented
💃
Elvis Nunez commented
Fixed on #2