Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegate a non-failable initializer to a failable initializer

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
}
like image 977
Yariv Nissim Avatar asked Apr 15 '15 16:04

Yariv Nissim


1 Answers

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
like image 50
Airspeed Velocity Avatar answered Sep 30 '22 15:09

Airspeed Velocity