繁体   English   中英

在Objective-C中选择一个随机枚举

[英]Choosing a random enum in Objective-C

我为我的iPhone应用程序创建了一个typedef枚举...

typedef enum {
    FirstType,
    SecondType,
    ThirdType
} type;

仅仅为了测试,我希望能够从这些中选择随机类型。

我打算使用arc4random() % 4来做它,只是在它的位置使用int,但想检查是否有更好的方法来做到这一点。

typedef enum {
    FirstType = 0,
    SecondType,
    ThirdType,
    EnumTypeMax
} EnumType;

EnumType randomType = (EnumType) (arc4random() % (int) EnumTypeMax);

请注意, EnumTypeMax等于3 ,而不是4 ,这是正确的。 EnumTypeMax被视为无效值。

另外,请在此处查看有关X-Macros解决方案的答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM