IPhone switch statement using an enumeration

I defined an enumeration in the class header file:

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

- (void) switchTest:(Colors)testColor;

and in the implementation file:

- (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 as soon as the first line of the switch starts, the application unexpectedly quits and without errors / errors.

I am not opposed to using if / else syntax, but I would like to understand my mistake.

+3
source share
3 answers

Works great 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;
}

+10
source

In my case, the problem was that I defined the variable:

Colors *testColor; //bad

Instead:

Colors testColor; //right

Hope this helps.

+5
source

. switch, case , - #ifdef. case , "" NSLog.

, - , . . xcode .

0

Source: https://habr.com/ru/post/1748211/


All Articles