【问题标题】:How do I iterate over an enum in Haxe?如何迭代 Haxe 中的枚举?
【发布时间】:2014-03-19 19:56:35
【问题描述】:

我有一个枚举类型:

enum PlayerProps {
    Attempts;
    Gold;
    Diamonds;
}

我应该怎么做才能遍历所有枚举值?比如:

var props = new Map<PlayerProps, Int>();
for (prop in PlayerProps)
    props[prop] = 0;

【问题讨论】:

    标签: enums iterator haxe


    【解决方案1】:

    你要找的是Type.allEnums():

    for (prop in Type.allEnums(PlayerProps))
    

    try.haxe.org 上的工作示例。

    【讨论】:

    • 谢谢,我稍后也找到了 allEnums。但是现在我了解到 Map 不能使用枚举类型作为键...
    • 哦,我不测试这个不好,我认为您可以通过使用枚举的 int 值找到解决方法(它应该可以通过某种方式访问​​)但我想这不是最好的解决方案这。您是否考虑过创建对象而不是地图?您可以定义一个类作为每个属性的字段(这可能会或可能不会满足您的需要)
    • 我正在尝试使用 IntMap 和 Type.enumIndex 方法找到解决方法。但这看起来很笨拙。
    • @meps 地图中的枚举应该可以工作,它们应该使用:haxe.ds.EnumValueMap。如果您有问题,请尝试将其输入为var mProps:EnumValueMap&lt;PlayerProps,Int&gt; = new EnumValueMap(),看看是否有不同...
    猜你喜欢
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 2011-10-06
    • 2017-02-03
    相关资源
    最近更新 更多