1

I have this type of Enum with TypeDef:

typedef enum {
    ControlDisplayOptionNone = 0,
    ControlDisplayOptionOne = 100
} ControlDisplayOption;

And I'd like to be able to put them in an array like this:

- (NSArray *)displayOptions {
    return @[@ControlDisplayOptionNone];
}

but that won't work, and even this won't work:

NSNumber *test = @ControlDisplayOptionNone;

the only option that will work is traditional:

return @[[NSNumber numberWithInt:ControlDisplayOptionNone]];

Is there any way to use autoboxing for this?

Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421

1 Answers1

5

Use parentheses: @(ControlDisplayOptionNone)

The syntax is explained in the Clang documentation for Objective-C Literals. The "Boxed Enums" section says:

Cocoa frameworks frequently define constant values using enums. Although enum values are integral, they may not be used directly as boxed literals (this avoids conflicts with future '@'-prefixed Objective-C keywords). Instead, an enum value must be placed inside a boxed expression.

benzado
  • 82,288
  • 22
  • 110
  • 138
  • Why I have to come back in 5 minutes to accept this answer I don't know. But while we're here, is there a place that you know this from (citation)? THANKS this works. – Dan Rosenstark Jan 15 '13 at 17:35
  • @Yar Citation added! So there you go, you need parens in case there is someday an Objective-C keyword `@ControlDisplayOptionNone` :-) – benzado Jan 15 '13 at 17:40