有没有办法将枚举rawValue向下转换为其类型?(Is there a way to down cast an enum rawValue to its type?)
我有以下枚举:
enum Difficulty: Int { case Opt1 = 1, Opt2 = 2, Opt3 = 3 }当我尝试将它用作Int那样的时候: MyClass.Difficulty.Opt1.rawValue它不允许我,因为它返回一个RawValue。 理解这是一个Int不应该是明智的吗?
I have the following enum:
enum Difficulty: Int { case Opt1 = 1, Opt2 = 2, Opt3 = 3 }When I try to use it as an Int like that: MyClass.Difficulty.Opt1.rawValue it doesn't allow me, because it returns a RawValue. Shouldn't it be smart to understand that this is an Int?
最满意答案
鉴于你的代码,我假设你有一个嵌套在类中的枚举,如下所示:
class MyClass { enum Difficulty: Int { case Opt1 = 1, Opt2 = 2, Opt3 = 3 } } var myInteger = MyClass.Difficulty.Opt2.rawValue // Int inferred correctly我发现这个或任何我能想到的变种都没有任何问题。 如果您仍然遇到问题,请发布MyClass类型的代码。
Given your code I assume that you have an enum nested inside a class like so:
class MyClass { enum Difficulty: Int { case Opt1 = 1, Opt2 = 2, Opt3 = 3 } } var myInteger = MyClass.Difficulty.Opt2.rawValue // Int inferred correctlyI'm not finding any problems with this or any variants I can think of. Please post your code for the MyClass type if you are still encountering problems.
更多推荐
发布评论