如何从课外访问类枚举?
class Element
{
enum Type
{
case AUDIO
case LIGHT
case THERMOSTAT
}
}
var a = Element.Type.LIGHT // error: 'Element.Type.Type' does not have a member named 'LIGHT'
var b = Element.LIGHT // error: 'Element.Type' does not have a member named 'LIGHT'
答案 0 :(得分:9)
Type
属性已存在(不幸名称:)),只需将其重命名为其他内容,例如:
class Element
{
enum EnumType
{
case AUDIO
case LIGHT
case THERMOSTAT
}
}
var a = Element.EnumType.LIGHT
答案 1 :(得分:0)
可以保留名称Type
,但您需要反引号(`)
class Element {
enum `Type` {
case AUDIO
case LIGHT
case THERMOSTAT
}
}
let a: Element.`Type` = .LIGHT
但是我测试的以下模式似乎无效:
let a = Element.`Type`.LIGHT // Compile error