如果条件转换为switch语句,如何更改?

时间:2012-08-14 09:27:38

标签: iphone objective-c ios switch-statement

我尝试将以下代码从if语句更改为switch语句,但是我得到一个错误,说我需要一个整数而不是id对象。

这是我的开关:

-(void) nearestIndexAfterRotationEnded:(int)index {
NSLog(@"Selected item is: %@", [colorNames objectAtIndex:index]);
statusLabel.text = [NSString stringWithFormat:@"Selected item is: %@", [colorNames objectAtIndex:index]];

switch ([colorNames objectAtIndex:])) {
    case 1:
        [redButton setImage:[UIImage imageNamed:@"Btn1_1.png"] forState:UIControlStateNormal];

        break;

    case 2:
        [redButton setImage:[UIImage imageNamed:@"Btn1_2.png"] forState:UIControlStateNormal];

        break;

    default:
        break;
}

以及我的if条件声明:

-(void) nearestIndexAfterRotationEnded:(int)index {
NSLog(@"Selected item is: %@", [colorNames objectAtIndex:index]);
statusLabel.text = [NSString stringWithFormat:@"Selected item is: %@", [colorNames objectAtIndex:index]];


if ([colorNames objectAtIndex:0]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_1.png"] forState:UIControlStateNormal];

}

if ([colorNames objectAtIndex:1]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_2.png"] forState:UIControlStateNormal];

}

if ([colorNames objectAtIndex:2]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_3.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:3]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_4.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:4]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_5.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:5]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_6.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:6]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_7.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:7]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_8.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:8]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_9.png"] forState:UIControlStateNormal];
}

}

感谢您的帮助

6 个答案:

答案 0 :(得分:5)

- (void)nearestIndexAfterRotationEnded:(int)index {
    NSLog(@"Selected item is: %@", [colorNames objectAtIndex:index]);
    statusLabel.text = [NSString stringWithFormat:@"Selected item is: %@", [colorNames objectAtIndex:index]];

    for (int i=0; i<9; i++) {
        if ([colorNames objectAtIndex:i]) {
            [redButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Btn1_%d.png", i+1]] forState:UIControlStateNormal];
        }
    }
}

答案 1 :(得分:1)

您无法将其更改为switch语句,因为它不符合交换机的先决条件。

开关始终遵循模式

switch aVar {

case value1:
case value2:
default: 

}

如果您坚持使用switch语句,则必须找到替代算法。

答案 2 :(得分:1)

Change it the code 
 switch ([colorNames objectAtIndex:]))
to 
switch (index)  This will be helpful for you.

答案 3 :(得分:1)

好吧,我无法回答,但大多数人已经说过了。

这是您想要更改为switch语句的原始if语句:

-(void) nearestIndexAfterRotationEnded:(int)index {
NSLog(@"Selected item is: %@", [colorNames objectAtIndex:index]);
statusLabel.text = [NSString stringWithFormat:@"Selected item is: %@", [colorNames objectAtIndex:index]];


if ([colorNames objectAtIndex:0]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_1.png"] forState:UIControlStateNormal];

}

if ([colorNames objectAtIndex:1]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_2.png"] forState:UIControlStateNormal];

}

if ([colorNames objectAtIndex:2]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_3.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:3]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_4.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:4]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_5.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:5]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_6.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:6]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_7.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:7]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_8.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:8]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_9.png"] forState:UIControlStateNormal];
}
}

现在这是不可能的,因为你需要一个变量来切换。

现在,所有答案都没有看到,你在if语句中所做的事情与传递的索引变量无关。它永远不会出现在那里。 (我认为这是错误的)

而是从0-8检查colorNames数组中索引0-8处是否存在对象。 如果任何if语句失败,则意味着以下所有内容都将失败,因为

if ([colorNames objectAtIndex: i]) 

返回NIL,这意味着这是数组的结尾!

因此检查if([colorNames objectAtIndex:0-8])是否毫无意义或至少要求简化。这整个if语句可以压缩为一行代码:

        [redButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Btn1_%d.png", colorNames.count]] forState:UIControlStateNormal];

也就是说,如果colorNames不包含超过9个对象。如果它拥有超过9个对象并且您仍然只想检查前9个是否存在,那么这一行代码可以解决这个问题:

[redButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Btn1_%d.png", MAX(9,colorNames.count)]] forState:UIControlStateNormal]; 

请记住,在整个if-thing中,传入的索引变量不会被考虑在内。

如果这是错误的并且你想在索引处返回数组中对象的名称,那么也不需要多个ifs:

if ([colorNames objectAtIndex: index])
 [redButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Btn1_%d.png", index+1]] forState:UIControlStateNormal];

所以我认为,您需要澄清您的问题(因为您的代码段无法完成)或改进您的代码。

答案 4 :(得分:0)

您必须在交换机中使用int。因此,您必须将[colorNames objectAtIndex:1]转换为整数。我不知道它们的颜色是什么,无论它们是NSNumber还是NSString,但我会假设它是最新的。

你必须像这样切换:

switch ([[colorNames objectAtIndex:index] intValue]))

intValue消息告诉NSString返回它的整数表示。那样你就可以换了。

答案 5 :(得分:-2)

你能运行这段代码吗?

switch ([colorNames objectAtIndex:])) {

应该抛出错误。您没有将值传递给objectAtIndex!

应该是

switch ([colorNames objectAtIndex:index])) {