获取枚举项的总数 [英] Getting total number of enum items
本文介绍了获取枚举项的总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在运行时获取由枚举定义的项目总数?
Is it possible to get the total number of items defined by an enum at runtime?
尽管与这一个,这个问题与C#有关,据我所知,该方法只要在Objective-C中不起作用。
While it's pretty much the same question as this one, that question relates to C#, and as far as I can tell, the method provided there won't work in Objective-C.
推荐答案
一个枚举
是一个普通的C类型,因此它不提供动态的运行时信息。
An enum
is a plain-old-C type, therefore it provides no dynamic runtime information.
另一种方法是使用枚举的最后一个元素来表示计数: / p>
One alternative is to use the last element of an enum to indicate the count:
typedef enum {
Red,
Green,
Blue,
numColors
} Color;
这篇关于获取枚举项的总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文