AyushISM
AyushISM

Reputation: 381

How To Compare Integer to Objective-C enum

- (void)updateCheckBoxes {
    NSArray *availableFuncUnits = _scanner.availableFunctionalUnitTypes;
    for(int i = 0; i < [availableFuncUnits count]; i++) {

    }
}

If I put a breakpoint inside the for loop, the elements of the NSArray * 'availableFuncUnits' are (__NSCFNumber *)(int)0 and (__NSCFNumber *)(long)3.

The array is supposed to contain elements of the following :

enum
{
    ICScannerFunctionalUnitTypeFlatbed              = 0,
    ICScannerFunctionalUnitTypePositiveTransparency = 1,
    ICScannerFunctionalUnitTypeNegativeTransparency = 2,
    ICScannerFunctionalUnitTypeDocumentFeeder       = 3
};
typedef NSUInteger ICScannerFunctionalUnitType; 

Shouldn't I be able to do the following?

if([availableFuncUnits objectAtIndex:i] == ICScannerFunctionalUnitType.ICScannerFunctionalUnitTypeDocumentFeeder) {}

But it always gives me an error saying 'Expected identifier or '('.

How can I perform this comparison correctly? Thanks a lot for the help!

Upvotes: 3

Views: 6713

Answers (2)

Paul Patterson
Paul Patterson

Reputation: 6918

You can't store integers in an NSArray because array's can only contain objects. To get integers into an array they must be wrapped with NSNumber:

NSInteger a = 100;
NSInteger b = 200;
NSInteger c = 300;

// Creating NSNumber objects the long way 
NSArray *arrayOne = [NSArray alloc] initWithObjects:[NSNumber numberWithInteger:a],
                                                    [NSNumber numberWithInteger:b],
                                                    [NSNumber numberWithInteger:c], nil];
// Creating NSNumber objects the short way
NSArray *arrayTwo = [[NSArray alloc] initWithObjects:@100, @200, @300, nil];

This is relevant you your question because when you extract your NSNumber objects from your array, if you want to then compare them to actual integers, you must convert them back to integers (unwrap them).

NSLog(@"%d", [arrayOne firstObject] == 100); // COMPILER WARNING!!!
NSLog(@"%d", [[arrayOne firstObject] integerValue] == 100); // True
NSLog(@"%d", [[arrayTwo lastObject] integerValue] == 200);  // False

This stage appears to be missing in your example.

Finally to compare your integer values with those from an enum, there's no need to reference the enum name, just use the individual values that make up the enum:

[[arrayTwo lastObject] integerValue] == ICScannerFunctionalUnitTypeFlatbed

Upvotes: 1

Aris
Aris

Reputation: 1559

There are two problems that I see:
1) The array availableFuncUnits contains NSNumber objects. You cant directly compare them with primitive types (NSUInteger).

So your if should be like this:

ICScannerFunctionalUnitType type = [availableFuncUnits[i] integerValue]
if(type == ICScannerFunctionalUnitTypeDocumentFeeder){}

In your snippet you were comparing the pointer, not the object.

2) The error you were seeing is because the proper way to use enums is:

i = ICScannerFunctionalUnitTypeDocumentFeeder

Upvotes: 4

Related Questions