我尝试使用变量作为大小写匹配,但是我得到“表达式不是 Objective-C 中的整数。
是否可以以这种方式在开关中使用变量?
int count = [array count];
switch ([number to check]) {
case 0:
//first statement
break;
case 1 ... (count -1):
//somewhere between 1 and the next to last statement
//Basically the middle
break;
case count:
//Last statement
default:
break;
}
最佳答案
Objective-C(和 C)switch
仅支持每个 case
语句的单个原始常量值(或 TwoStraws 的答案中指出的范围)。使用 if/else
编写代码会更好:
if ([number to check] == 0) {
} else if ([number to check] >= 1 && [number to check] < count) {
} else if ([number to check] == count) {
} else {
}
关于objective-c - 在 switch 语句中使用变量作为 case 常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34362850/