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
}
While you can’t delegate to a failable initializer, since enums are value types, you can just try creating another value of the same enum using the failable initializer, and then substitute a default in case of nil
, and assign that to self
:
enum Result: Int {
case Success
case Failure
case Unknown = -1
init(value: Int) {
self = Result(rawValue: value) ?? .Unknown
}
}
Result(value: 100) == .Unknown // returns true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With