10

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
}
Yariv Nissim
  • 13,273
  • 1
  • 38
  • 44

1 Answers1

17

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
Airspeed Velocity
  • 40,491
  • 8
  • 113
  • 118
  • Thanks! I'm embarrassed I didn't think to use the type name in the initializer. – Yariv Nissim Apr 15 '15 at 23:47
  • Hah! I was insisting to make it work by trying with `self.init(....` ..no way. I felt odd when I saw the solution is simply `self = ` etc. :-) thanks. – Trevor Jun 03 '18 at 13:22
  • 1
    If anyone else is trying to create an initializer that delegates to a failable initializer but stops execution when it fails (e.g. preconditionFailure()) the following works: `init(unsafeValue: Int) { guard let result = Result(rawValue: unsafeValue) else { preconditionFailure("Unsafe value was not a valid result") } self = result }` – Nicholas Dec 05 '19 at 00:12
  • See https://stackoverflow.com/questions/67781425/use-self-in-convenience-initializers-to-delegate-to-jsondecoder-or-factory-m if you need to do something like this on a class where `self =` is unavailable. – deaton.dg Jun 11 '21 at 21:54
  • Although you can't say `self.init`, you can say `self = Self(....` instead of `self = Result(....`, although it's not necessarily clearer code! – Bluby Aug 14 '23 at 02:12