最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is there any way to take control of the shorthand syntax for enum literals in Swift? - Stack Overflow

programmeradmin2浏览0评论

In my app we have a enum that we do not have a lot of control over as it is auto generated.

It ends up looking like

enum State {
  case known(KnownStates)
  case unknown(UnknownState?)
}
enum KnownStates {
  case AK
  case AL
  case ON
  case WA
  ....
}

What I am wondering is if there is a way to regain the shorthand syntax of being able to do something like this

let knownState: KnownStates = .WA

But with my more complex structure

let state: State = .WA

I did try writing

   extension State {
     init(_ state: Components.Schemas.KnownStateOrProvince) {
        self = .KnownStateOrProvince(state)
     }
   }

But that does not allow the shorthand syntax to compile.

Obviously if these types weren't auto generated I would make something like

enum State {
  case AL
  case AK
  case ON
  case unknown(String?)
}

But I do not have that option.

发布评论

评论列表(0)

  1. 暂无评论