views:

139

answers:

1

I have defined an enum in a header file of a class :

typedef enum{
 RED = 0,
 BLUE,
 Green
} Colors;

- (void) switchTest:(Colors)testColor;

and in the implementation file I have :

- (void) switchTest:(Colors)testColor{

   if(testColor == RED){
    NSLog(@"Red selected");    
   }

   switch(testColor){
    case RED:
    NSLog(@"Red selected again !");
    break;
    default:
    NSLog(@"default selected");
    break;
   }

}

My code compiles correctly without warrnings. When calling the switchTest method with RED, the output is : "Red selected"

but once the first line of the switch runs, the application quits unexpectedly and without warrnings/errors.

I don't mind using if/else syntax but I would like to understand my mistake.

A: 

Works fine for me:

typedef enum{ RED = 0, BLUE, Green } Colors;

@interface Test : NSObject { } - (void) switchTest:(Colors)testColor; @end

@implementation Test

  • (void) switchTest:(Colors)testColor { if(testColor == RED) { NSLog(@"Red selected");
    }

    switch(testColor){ case RED: NSLog(@"Red selected again !"); break; default: NSLog(@"default selected"); break; } }

@end

int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

Test *myTest = [[Test alloc] init];

[myTest switchTest:RED];

[myTest switchTest:RED];



[pool drain];
return 0;

}

Derrick Johnson