OGeek|极客世界-中国程序员成长平台

标题: iphone - 没有输入switch语句? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:16
标题: iphone - 没有输入switch语句?

有一个我想不通的最奇怪的问题。在下面的方法中,没有输入 switch 语句。当我打印出 warningAlertViewType 的值时,它是正确的,但是由于某种原因 switch 语句没有激活。我以前使用过同样的方法进行切换,效果很好。

有谁知道这是怎么回事?

+ (WarningAlertView*) warningAlertViewWithTypeWarningAlertViewType)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];
    }



Best Answer-推荐答案


将您的 switch 语句更改为如下所示

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

我实际上是这样严格命名的粉丝。然后可以使用宏(我知道有些人讨厌)来大大缩短它。

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

}

关于iphone - 没有输入switch语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15913905/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4