xcode if else 语句的预期表达式错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11739663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expected expression error for if else statement
提问by JSA986
Xcode complains about " Expected expression required" for second else if
statement in my code. Ive tried using parenthesis from code I had looked at on here but that didn't work and not sure what expression it wants now? Works fine until I add a second else if
I realise this is probably basic objective C stuff but having never done an if statement for more then two items im a bit stuck
Xcode 对else if
我的代码中的第二个语句抱怨“需要预期的表达式” 。我试过使用我在此处查看的代码中的括号,但这不起作用,并且不确定它现在想要什么表达式?工作正常,直到我添加第二个else if
我意识到这可能是基本的目标 C 的东西,但从来没有为超过两个项目做过 if 语句我有点卡住了
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if ([text1 isFirstResponder])return arrStatus.count;
else
if ([text2 isFirstResponder]);return arrStatus2.count;
else ///<<<<< wants an expected expression here
if ([text3 isFirstResponder]);return arrStatus2.count;
}
回答by Alladinian
You just have an extra semicolon on your second if
statement.
It should be: if ([text2 isFirstResponder]) return arrStatus2.count;
您的第二个if
语句中只有一个额外的分号。它应该是:if ([text2 isFirstResponder]) return arrStatus2.count;
PS. You're making the same mistake on the 3rd if
as well... You should really consider using curly brackets even for one-liners.
附注。您在第 3 次if
也犯了同样的错误......即使对于单行,您也应该真正考虑使用大括号。
回答by trojanfoe
You have spurious semi-colons after some of the if
statements:
在某些if
语句之后,您有虚假的分号:
if ([text2 isFirstResponder]); <--- here
Other than that your code is poorly indented and you should stop putting statements on the same line as the if
/else if
lines:
除此之外,您的代码缩进很差,您应该停止将语句与if
/else if
行放在同一行:
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if ([text1 isFirstResponder])
return arrStatus.count;
else if ([text2 isFirstResponder])
return arrStatus2.count;
else if ([text3 isFirstResponder])
return arrStatus2.count;
}
回答by Pieter Gunst
Your function/method is required to return something, in this case an NSInteger. Yet there is a possible situation in your code where there is no return statement. In your last else, if the if statement is not correct, nothing will be returned.
你的函数/方法需要返回一些东西,在这种情况下是一个 NSInteger。然而,您的代码中可能存在没有 return 语句的情况。在你最后的 else 中,如果 if 语句不正确,则不会返回任何内容。
if ([text1 isFirstResponder]) {
return arrStatus.count;
} else {
if ([text2 isFirstResponder]) {
return arrStatus2.count;
} else {
if ([text3 isFirstResponder]) {
return arrStatus2.count;
}
// nothing will be returned here
// you can return nil or actual 0
// return nil;
// return 0;
}
}
}