Search code examples
objective-cenumsnsnumberobjective-c-literals

Can't instantiate NSNumber with shorthand @n type syntax when using enum?


I have an enum defined like this:

typedef enum dataTypes{
    LOW,
    MEDIUM,
    HIGH,
    MAX_DATA_TYPE
} dataTypeEnum;

I'd like to be able to instantiate an NSArray of NSNumbers like so:

NSArray * numsToUse = @[@LOW, @MEDIUM];

This is not compiling. Any insights? Do I have to go with the clunkier [NSNumber numberWithInt:] for each of these or is there a way around this? (I have considered and rejected #define statements for a number of reasons).


Solution

  • You just need to use the expression syntax:

    NSArray * numsToUse = @[@(LOW), @(MEDIUM)];