Ser Pounce
Ser Pounce

Reputation: 14571

Switch statement not being entered?

Have the most bizarre problem that I can't figure out. In the method below, the switch statement is not being entered. When I print out the value of warningAlertViewType, it's correct, but the switch statement is not activating for some reason. I've used this same methodology for a switch before and it's worked fine.

Does anyone know what might be going on to cause this?

+ (WarningAlertView*) warningAlertViewWithType:(WarningAlertViewType)warningAlertViewType
    {
        WarningAlertView *warningAlertView = nil;
        NSLog(@"WarningAlertViewType1: %d", warningAlertViewType);
        switch (warningAlertViewType)
        {
                NSLog(@"Test1");
            case WarningAlertViewTypeExit:                  warningAlertView = [[ExitWarningAlertView alloc] init]; break;
            case WarningAlertViewTypeFacebook:              warningAlertView = [[FacebookWarningAlertView alloc] init]; break;
            case WarningAlertViewTypeDelete:                warningAlertView = [[DeleteWarningAlertView alloc] init]; break;
            case WarningAlertViewTypePhotoLibrary:          warningAlertView = [[PhotoLibraryWarningAlertView alloc] init]; break;
            case WarningAlertViewTypeBack:                  warningAlertView = [[BackWarningAlertView alloc] init]; break;
            default: break;
        }
        NSLog(@"Test2");
        return [warningAlertView autorelease];
    }

Upvotes: 2

Views: 69

Answers (1)

epatel
epatel

Reputation: 46041

Change your switch statement to something like below

switch (warningAlertViewType) {

 case WarningAlertViewTypeExit:
   NSLog(@"WarningAlertViewTypeExit");
   warningAlertView = [[ExitWarningAlertView alloc] init];
   break;

 case WarningAlertViewTypeFacebook:
   NSLog(@"WarningAlertViewTypeFacebook");
   warningAlertView = [[FacebookWarningAlertView alloc] init];
   break;

 case WarningAlertViewTypeDelete:
   NSLog(@"WarningAlertViewTypeDelete");
   warningAlertView = [[DeleteWarningAlertView alloc] init];
   break;

 case WarningAlertViewTypePhotoLibrary:
   NSLog(@"WarningAlertViewTypePhotoLibrary");
   warningAlertView = [[PhotoLibraryWarningAlertView alloc] init];
   break;

 case WarningAlertViewTypeBack:
   NSLog(@"WarningAlertViewTypeBack");
   warningAlertView = [[BackWarningAlertView alloc] init];
   break;

 default:
   NSLog(@"default");
   break;
}

I am actually a fan of strict naming like this. Then one can use macros (that some hate, I know) to shorten this considerably.

switch (warningAlertViewType) {

#define CASE(_type) \
case WarningAlertViewType ## _type: \
  NSLog(@"WarningAlertViewType" #_type); \
  warningAlertView = [[_type ## WarningAlertView alloc] init]; \
  break

CASE(Exit);
CASE(Facebook);
CASE(Delete);
CASE(PhotoLibrary);
CASE(Back);

default:
  NSLog(@"default");
  break;

#undef CASE

}

Upvotes: 1

Related Questions