How to build an array of enums in Objective-C as NSNumber and then return these to enums again?

212 Views Asked by At

Having read several Q&As on this, it seems that the only way to build an array of predefined enums in Objective-C is to first convert to NSNumber. For example:

NSArray *fruits;

    fruits = [NSArray arrayWithObjects:
             [NSNumber numberWithInt:enumValue1],
             [NSNumber numberWithInt:enumValue2],
             [NSNumber numberWithInt:enumValue3],
             nil];

This solved my first problem and allowed me to add enums to the array - and then shuffle (randomise) their indices, which was the whole point of building an array in the first place. However, is there any way of re-converting these NSNumbers back to their original enum form, effectively reversing the conversion, so that they can be evaluated against the original enum values?

The alternative, to also convert the comparator to an NSNumber at every evaluation, is at the very least ugly. In addition, I am getting some warnings during these evaluations, such as:

Comparison between pointer and integer ('ORKPredefinedTaskMovementOption' (aka 'enum ORKPredefinedTaskMovementOption') and 'NSNumber * _Nonnull')

Many thanks in advance!!

0

There are 0 best solutions below