[英]Choosing a random enum in Objective-C
I've created a typedef enum for my iPhone app... 我为我的iPhone应用程序创建了一个typedef枚举...
typedef enum {
FirstType,
SecondType,
ThirdType
} type;
Just for testing I'd like to be able to pick a random type from these. 仅仅为了测试,我希望能够从这些中选择随机类型。
I was going to use arc4random() % 4
to do it and just use the int in its place but wanted to check if there was a better way of doing this. 我打算使用
arc4random() % 4
来做它,只是在它的位置使用int,但想检查是否有更好的方法来做到这一点。
typedef enum {
FirstType = 0,
SecondType,
ThirdType,
EnumTypeMax
} EnumType;
EnumType randomType = (EnumType) (arc4random() % (int) EnumTypeMax);
Note that EnumTypeMax
is equal to 3
, not 4
and that is correct. 请注意,
EnumTypeMax
等于3
,而不是4
,这是正确的。 EnumTypeMax
is considered to be an invalid value. EnumTypeMax
被视为无效值。
Also, see my answer here about an X-Macros solution. 另外,请在此处查看有关X-Macros解决方案的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.