0

我试图将以下代码从 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];
}

}

谢谢你的帮助

4

6 回答 6

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];
        }
    }
}
于 2012-08-14T09:32:22.463 回答
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 中,传入的索引变量没有被考虑在内。

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

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

所以我认为,您要么需要澄清您的问题(因为您的代码片段无法完成更多工作),要么需要改进您的代码。

于 2012-08-14T10:09:47.107 回答
1

您不能将其更改为 switch 语句,因为它不满足 switch 的先决条件。

开关始终遵循模式

switch aVar {

case value1:
case value2:
default: 

}

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

于 2012-08-14T09:32:23.623 回答
1
Change it the code 
 switch ([colorNames objectAtIndex:]))
to 
switch (index)  This will be helpful for you.
于 2012-08-14T09:34:37.273 回答
0

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

你必须像这样切换:

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

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

于 2012-08-14T09:33:24.377 回答
-2

你能运行这段代码吗?

switch ([colorNames objectAtIndex:])) {

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

它应该是

switch ([colorNames objectAtIndex:index])) {
于 2012-08-14T09:31:33.790 回答