这段代码看起来完全合乎逻辑。为什么我会收到“开关条件具有 boolean 值”错误?我用它来切换 UIButton
上的文字。它有效,但我仍然遇到错误。
bool buttonSunStatus = TRUE;
- (void) useButtonSun:(id)sender
{
buttonSun = [UIButton buttonWithType: UIButtonTypeCustom];
switch (buttonSunStatus) {
case TRUE:
[sender setTitle:@"No Sun" forState:UIControlStateNormal];
buttonSunStatus = FALSE;
break;
case FALSE:
[sender setTitle:@"Sun" forState:UIControlStateNormal];
buttonSunStatus = TRUE;
break;
}
}
最佳答案
警告只是告诉您您正在使用带有 boolean 值的 switch
语句。对于真正的 boolean 值,您应该只使用 if
-else
语句,而不是 switch
。
请注意,如果您使用 true boolean 类型,您只会收到此警告。如果您使用 BOOL
,则在 Mac OS 或非 64 位 iOS 上这是一个 unsigned char
。因此,您不会收到针对这些目标的警告。但是,如果您使用 bool
(或者如果您在 64 位 iOS 目标上使用 BOOL
),您将收到此警告。
您可以将 switch
语句替换为 if
-else
语句以抑制此警告。
关于objective-c - 开关条件有 boolean 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26411482/