警告:控制到达非空函数的结尾 - iPhone

时间:2010-03-13 22:47:49

标签: iphone objective-c xcode

我通过以下代码继续'警告:控制到达非空函数的结尾':

- (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];

我怎样摆脱这个警告?

感谢。

2 个答案:

答案 0 :(得分:60)

在方法的最后添加return 0;。如果没有满足if条件,它基本上是故障安全的。

如果要确保满足其中一个条件,return -1;应该导致应用程序抛出异常并崩溃,这可能有助于您追踪错误。

您可能还会考虑修改此方法并将if语句替换为switch-case树。使用switch-case树,您可以非常轻松地添加新部分,并在UITableView中重新排列顺序。通过使用合理的命名约定,代码变得非常容易阅读。

这很容易; Fraser Speirs有一个good explanation on how to set this up

答案 1 :(得分:4)

一个选项:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (section ==0) {
        return [comparativeList count];
    } else if (section==1) {
        return [generalList count];
    }
    // section == 2    
    return [contactList count];
}