Use reserved keyword a enum case

后端 未结 1 1027
我在风中等你
我在风中等你 2020-11-29 11:19

Is it possible to use a reserved keyword as enum case?

For example:

enum MyEnum {
  case Self // compiler complains here
  case AnotherCase
}
         


        
相关标签:
1条回答
  • 2020-11-29 11:42

    From the Swift Language Guide (Naming Constants & Variables section)

    If you need to give a constant or variable the same name as a reserved Swift keyword, surround the keyword with back ticks (`) when using it as a name. However, avoid using keywords as names unless you have absolutely no choice.

    enum MyEnum {
      case `Self`
      case AnotherCase
    }
    

    and use it with or without backticks

    let x: MyEnum = .Self
    let y = MyEnum.`Self`
    
    0 讨论(0)
提交回复
热议问题