如何停止重复访问关联类型枚举的值的代码?

use*_*ser 0 swift

我想访问我的枚举的值,以防万一,我做了一个切换来访问值,但正如你所看到的,我正在收获我的代码,我怎样才能以更好的方式做到这一点?

enum TestEnum {
    case a(value: Int)
    case b(value: Int)
    case c(value: Int)
    
    var value: Int {
        switch self {
        case .a(let value):
            return value
        case .b(let value):
            return value
        case .c(let value):
            return value
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Emi*_*aez 5

也许您可以改变数据建模​​的方式

struct MyStruct {
  enum Kind {
    case one, two, three
  }
  
  let kind: Kind
  let value: Int
}
Run Code Online (Sandbox Code Playgroud)