Matt Weinecke Matt Weinecke - 6 months ago 13
Swift Question

Is it possible to add associated values to an existing Swift enum via extensions?

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?

hnh hnh

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 }