枚举可以在Swift中包含另一个枚举值吗?

我想分享一些枚举属性.就像是:
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
  }
}

我明白为什么ActionState不能从State继承(因为被取消的状态在State中没有表示)但我仍然想说“ActionState就像有更多选项的State,而ActionState可以获得State类型的输入,因为它们也属于ActionState类型“

我看到如何使用上述逻辑来处理在ActionState中复制案例并在set函数中进行切换.但我正在寻找更好的方法.

我知道枚举不能在Swift中继承,我已经阅读了swift-enum-inheritance的协议答案.它没有解决“继承”或包含来自另一个枚举的案例的需要,而只涉及属性和变量.

固定代码
import Foundation

enum State {
    case started
    case succeeded
    case Failed
}

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

class Action {

    var state: ActionState = .state(value: .started)

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

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

    var description:String {

        var result = "Action "
        switch state {

            case .state(value: .started):
                result += "\(state)"

            case .state(value: _):
                result += "\(state)"

            case .cancelled:
                result += "cancelled"
        }
        return result
    }
}

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

结果

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...