Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

objective c - Getting total number of enum items

Is it possible to get the total number of items defined by an enum at runtime?

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.

question from:https://stackoverflow.com/questions/1469358/getting-total-number-of-enum-items

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

An enum is a plain-old-C type, therefore it provides no dynamic runtime information.

One alternative is to use the last element of an enum to indicate the count:

typedef enum {
    Red,
    Green,
    Blue,
    numColors
} Color;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...