Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top