小编sho*_*ker的帖子

使用枚举的iPhone开关语句

我在类的头文件中定义了一个枚举:

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

- (void) switchTest:(Colors)testColor;
Run Code Online (Sandbox Code Playgroud)

在我的实施文件中:

- (void) switchTest:(Colors)testColor{

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

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

}
Run Code Online (Sandbox Code Playgroud)

我的代码正确编译而无需更改.使用RED调用switchTest方法时,输出为:"Red selected"

但是一旦交换机的第一行运行,应用程序就会意外退出并且没有保修/错误.

我不介意使用if/else语法,但我想了解我的错误.

iphone objective-c

5
推荐指数
2
解决办法
2万
查看次数

标签 统计

iphone ×1

objective-c ×1