Matt Weinecke
Matt Weinecke

Reputation: 602

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?

Upvotes: 2

Views: 217

Answers (1)

hnh
hnh

Reputation: 14805

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 }

Upvotes: 1

Related Questions