这里有什么问题?为什么我不能用"switch"?
NSString *input;
gets(charInput);
input=[NSString stringWithUTF8String:charInput];
switch (input) { //Statement requires expression of integer type ('NSString *__strong' invalid)
case "test": //Expression is not an integer constant expression
NSLog(@"Yes it is");
break;
case "Hello": //Expression is not an integer constant expression
NSLog(@"Hey There!");
default:
break;
谢谢!
在Objective C中
Switch语句只能传递int形参。
这就是为什么你会得到这个错误。
希望这对你有帮助!
这是因为,errr, "switch"语句要求表达式为整型。它只允许你使用数字。
实际上,这个限制只存在于Objective-C中。如果你使用SWIFT,你可以使用字符串,如下页所示:
迅速凸显
(向下滚动到"Switch statements"部分)