我想分享一些枚举属性.就像是:
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
}
}
Run Code Online (Sandbox Code Playgroud)
我明白为什么ActionState不能继承State(因为状态cancelled没有表示State)但我仍然可以说"ActionState就像具有更多选项的State,而ActionState可以获得State类型的输入,因为它们也是输入ActionState"
我看到如何使用上述逻辑来复制案例ActionState并在set函数中进行切换.但我正在寻找一种更好的方法.
我知道枚举不能在Swift中继承,我已经阅读了swift-enum-inheritance的协议答案.它没有解决"继承"或包含来自另一个枚举的案例的需要,而只涉及属性和变量.