【发布时间】:2017-08-04 09:09:03
【问题描述】:
我正在尝试创建一个类并将该类用作我的新枚举的类型,如下所示。
class Abc{
var age = 25
var name = "Abhi"
}
enum TestEnum : Abc {
case firstCase
case secondCase
}
我在操场上遇到以下错误。
error: raw type 'Abc' is not expressible by any literal
所以我尝试像这样遵循 RawRepresentable 协议。
extension TestEnum : RawRepresentable{
typealias RawValue = Abc
init?(rawValue:RawValue ) {
switch rawValue {
case Abc.age :
self = .firstCase
case Abc.name :
self = .secondCase
}
}
var rawValue : RawValue {
switch self{
case .firstCase :
return Abc.age
case .secondCase :
return Abc.name
}
}
}
在此之后我收到以下错误:
error: raw type 'Abc' is not expressible by any literal
error: instance member 'age' cannot be used on type 'Abc'
error: instance member 'name' cannot be used on type 'Abc'
声明某种类类型的枚举的正确方法是什么,对此并不清楚。有人帮忙吗?
【问题讨论】:
-
enum是值类型而class是引用类型,所以我认为不可能。 -
你想要达到什么目的?
-
来自文档:“原始值可以是字符串、字符或任何整数或浮点数类型”
-
@MartinR,您可以使用自定义类型作为原始值。看我的回答。
-
就像任何其他预定义类型一样,我想要一个我自己类型的枚举。类类型。