【问题标题】:How do I change this if conditional into a switch statement?如果有条件,如何将其更改为 switch 语句?
【发布时间】:2012-08-14 09:27:37
【问题描述】:

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

}

感谢您的帮助

【问题讨论】:

  • 你将什么类的对象放入 colorNames 中?
  • 你到底想做什么?
  • 我希望根据选择的数组中的对象来更改按钮图像。它是一个旋转轮,当用户旋转它时,按钮图像应该会改变
  • 你甚至不需要 switch 语句......为什么不直接使用 [NSString stringWithFormat:@"Btn1_%d.png", index];

标签: iphone objective-c ios switch-statement


【解决方案1】:
- (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];
        }
    }
}

【讨论】:

  • 好的,但是为什么要循环呢?如我所见,它是 [UIImage imageNamed:[NSString stringWithFormat:@"Btn1_%d.png", colorNames.count]]
  • colorNames.count - 它是静态变量,我们有动态图像名称
  • 不太明白你的意思。计数应该有效,我猜这里的数组不超过 9 个项目。那里没有对象可以为零,因此循环的 if 语句本质上只是检查计数。当然,可能有超过 9 个项目(我对此设置表示怀疑)。然后使用 MAX(9,colorNames.count) 代替 colorNames.count 来解决这个问题。
  • 我相信@hanumanDev 知道他在做什么。所以,索引约束不是这个问题的问题。
  • 坦率地说,我认为他不太了解自己在做什么;)而且,循环部分似乎没有必要,因为使用计数(或计数/最大 9)与您的操作相同环形。不过好吧,他似乎对 switch 索引答案很满意
【解决方案2】:
Change it the code 
 switch ([colorNames objectAtIndex:]))
to 
switch (index)  This will be helpful for you.

【讨论】:

  • 你不知道 colorName objectAtIndex:index 是否会匹配索引值。
  • @hanumanDev 每当您的颜色名称更改数组内的顺序时,它就会停止工作。你应该意识到这一点。
  • 也不同意这个答案。我们只有一个代码 sn-p,但实际上,在决定显示哪个图像时不会包含索引。
【解决方案3】:

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

切换始终遵循模式

switch aVar {

case value1:
case value2:
default: 

}

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

【讨论】:

  • 唯一一个看到提供的代码 sn-p 没有考虑索引变量的人(见我的回答)- PS:你好,德国 :)
【解决方案4】:

好吧,我忍不住回答了,虽然大部分都已经说了。

这是您要更改为 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,表示这是数组的结尾!

所以检查 ([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];

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

【讨论】:

    【解决方案5】:

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

    你必须像这样切换:

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

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

    【讨论】:

    • 嗯,没错,但似乎不能解决问题。
    • @HermannKlecker 没有解决哪个问题?他收到一条错误消息,上面写着“我需要一个整数而不是 id 对象”。这解决了错误。为什么要投反对票?
    • 是的,但由于某种原因,他想处理第一个分支中索引 1 处的对象和第二个分支中索引 2 处的对象。这不能通过开关来实现,当然不是你的建议。我猜 NeverBe 对 for 循环的建议最接近解决问题。 humanDev 可能希望在 for 循环中的 if 中添加一个 break 语句,具体取决于他真正想要实现的目标(他还没有告诉我们)。
    【解决方案6】:

    你能运行这段代码吗?

    switch ([colorNames objectAtIndex:])) {
    

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

    应该是

    switch ([colorNames objectAtIndex:index])) {
    

    【讨论】:

    • 如果我尝试: switch ([colorNames objectAtIndex:index]) { case 0: [redButton setImage:[UIImage imageNamed:@"Btn1_1.png"] forState:UIControlStateNormal];休息;案例1:[redButton setImage:[UIImage imageNamed:@"Btn1_2.png"] forState:UIControlStateNormal];休息;默认值:中断;我得到一个“声明需要整数类型的表达式('id'无效)”
    • 你不能从一个 NSArray 中切换一个对象,一个 switch 语句需要一个整数值,所以这永远不会工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 2021-12-18
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多