Edit
A simple solutions was found thanks to @Airspeed Velocity, with a little twist added since this is parsed from JSON. Allow the initializer to take AnyObject?
and default to Unknown
(or Invalid
):
init(value: AnyObject?) {
if let value = value as? Int
,let result = Result(rawValue: value) {
self = result
} else {
self = .Unknown
}
}
//
Original
I've implemented an enum with a Unknown
case:
enum Result: Int {
case Success
case Failure
case Foo
case Bar
case FooBar
case FooFoo
...
case Unknown = -1
}
I want to create another initializer that takes an Int
and returns Unknown
for case not recognized by the enum:
init(value: Int) {
self.init(rawValue: value)
if self == nil { // Complication Error
self = .Unknown
}
}
My solution for now is a Factory method, however an initializer would be much cleaner:
static func resultWithValue(value: Int) -> Result {
if let result = self(rawValue: value) {
return result
}
return .Unknown
}