问题描述
我通过以下代码继续'警告:控制到达非空函数的结尾':
I keep getting 'warning: control reaches end of non-void function' with this code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section ==0)
{
return [comparativeList count];
}
if (section==1)
{
return [generalList count];
}
if (section==2)
{
return [contactList count];
如何摆脱此警告?
谢谢。
推荐答案
在此处添加返回0;
方法结束。如果没有满足 if
条件,它基本上是故障安全的。
Add a return 0;
at the end of your method. It's basically a failsafe, if none of the if
conditions are met.
如果你想确保满足其中一个条件, return -1;
应该导致应用程序抛出异常并崩溃,这可能会帮助您追踪错误。
If you want to make sure one of the conditions is met, return -1;
should cause the application to throw an exception and crash, which might help you track down errors.
您可能还会考虑修改此方法并替换 if
带有 switch-case
树的语句。使用 switch-case
树,您可以非常轻松地添加新部分并在 UITableView
中重新排列其顺序。通过使用合理的命名约定,代码变得非常容易阅读。
You might also look at modifying this method and replacing the if
statements with a switch-case
tree. With a switch-case
tree, you can very easily add new sections and rearrange their order in a UITableView
. The code becomes trivially easy to read by using sensible naming conventions.
这很容易; Fraser Speirs有一个。
It's really easy; Fraser Speirs has a good explanation on how to set this up.
这篇关于警告:控制到达无效功能的结束 - iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!