Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an enum contain another enum values in Swift?

Tags:

enums

swift

I'd like to share some enum properties. Something like:

enum State {
  case started
  case succeeded
  case failed
}

enum ActionState {
  include State // what could that be?
  case cancelled
}

class Action {
  var state: ActionState = .started

  func set(state: State) {
    self.state = state
  }

  func cancel() {
    self.state = .cancelled
  }
}

I see why ActionState can not inherit from State (because the state cancelled has no representation in State) but I want to still be able to say "ActionState is like State with more options, and ActionState can get inputs that are of type State, because they are also of type ActionState"

I see how I could get the above logic to work with copying the cases in ActionState and having a switch in the set function. But I'm looking for a better way.

I know enum can't inherit in Swift, and I've read the protocol answer of swift-enum-inheritance. It doesn't address the need for "inheriting" or including cases from another enum, but only properties and variables.

like image 672
Guig Avatar asked Nov 17 '16 21:11

Guig


People also ask

Can enum have another enum?

Enum is a special class, with special behavior. You can't directly inherit this class, but it's inderectly inheriter any time you declare a new enum . You cannot inherit one new enum from other enum .

Can enum inherit from another enum Swift?

So it is not possible to have a certain Enum extend and inherit the cases of another enum.

Can two enum values be the same?

Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.

What is difference between enum and enumeration in Swift?

Enumeration is a data type that allows you to define a list of possible values. An enum allows you to create a data type with those set of values so that they can be recognised consistently throughout your app.


1 Answers

Details

  • Swift 4, 3
  • Xcode 10.2.1 (10E1001), Swift 5 (Last revision of this post)

Solution

enum State {
    case started, succeeded, failed
}

enum ActionState {
    case state(value: State), cancelled
}

class Action {
    var state: ActionState = .state(value: .started)
    func set(state: State) { self.state = .state(value: state) }
    func cancel() { state = .cancelled }
}

Full Sample

Do not to forget to paste the solution code

import Foundation

extension Action: CustomStringConvertible {
    var description: String {
        var result = "Action - "
        switch state {
            case .state(let value): result += "State.\(value)"
            case .cancelled: result += "cancelled"
        }
        return result
    }
}

let obj = Action()
print(obj)
obj.set(state: .failed)
print(obj)
obj.set(state: .succeeded)
print(obj)
obj.cancel()
print(obj)

Result

//Action - State.started
//Action - State.failed
//Action - State.succeeded
//Action - cancelled
like image 88
Vasily Bodnarchuk Avatar answered Sep 17 '22 14:09

Vasily Bodnarchuk