iphone - 没有输入 Switch 语句?

标签 iphone ios objective-c cocoa-touch

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

有谁知道是什么原因导致的?

+ (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];
    }

最佳答案

将您的 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/

相关文章:

ios - Xcodebuild Shell 错误的 OCLint

ios - 如何根据Apple ID在iOS中检查用户是否已购买或未使用应用内购买

ios - 自动布局 : UITextFields with equal width in different UITableViewCells

objective-c - 如何使用 AVPlayer 播放 mp4 资源?

ios - ScrollView 的 UIbutton 不交互

ios - 解决两个平移手势识别器之间的冲突

ios - 第一次尝试实现键值观察,出现一些错误

iphone - iPhone iOS 上的锁屏广告应用 Apple iAds 指南

iphone - 如何更改 iphone 的 UISearchBar 中的光标颜色?

iphone - 将 webview 中的照片保存到 ios 照片库