sam-w
sam-w

Reputation: 7687

Is there an equivalent to C++'s class-scoped enum?

In C++ I might do the following in a header:

cClass {
  enum eList { FIRST, SECOND };
}

... and this in some other class:

cClass::eList ListValue = GetListValue();
if(ListValue == cClass::FIRST) {
  ...
}

Is there an equivalent either using straight Objective-C language features or some trickery in Cocoa which would allow similarly scoped enums?

Upvotes: 2

Views: 103

Answers (1)

justin
justin

Reputation: 104698

Well, you can emulate parts of it using C:

create a C enumeration and type:

enum MONEnumType : uint8_t {
  MONEnumType_Undefined = 0,
  MONEnumType_Red,
  MONEnumType_Green,
  MONEnumType_Blue
};

declare container:

struct MONEnum {
  const enum MONEnumType Red, Green, Blue;
};

declare storage:

extern const struct MONEnum MONEnum;

define storage:

const struct MONEnum MONEnum = {
  .Red = MONEnumType_Red,
  .Green = MONEnumType_Green,
  .Blue = MONEnumType_Blue
};

in use:

enum MONEnumType ListValue = GetListValue();
if (ListValue == MONEnum.Red) {
  /* ... */
}

Upvotes: 3

Related Questions