有没有办法将枚举rawValue向下转换为其类型?(Is there a way to down cast an enum rawValue to its type?)

编程入门 行业动态 更新时间:2024-10-23 03:22:07
有没有办法将枚举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 correctly

I'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.

更多推荐

本文发布于:2023-08-04 14:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417691.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:为其   没有办法   类型   rawValue   type

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!