2

In Swift 3, say there is a system defined enum (i.e. I don't control the source code) like this:

enum currentState: Int {
  case enabled
  case disabled
  case unknown
}

Is it possible to add an associated value to the existing members specifically via extensions?

Matt Weinecke
  • 602
  • 6
  • 17

1 Answers1

1

No you can't. Think about it, the currentState enum really is just an restricted Int. It's not even an object.

If you need a more complex enum, you need to wrap the one given to you.

Note: Swift extensions cannot add state to the base type in general. E.g. this doesn't work either:

class A {}
extension A { var value : Int = 32 }
hnh
  • 13,957
  • 6
  • 30
  • 40