【问题标题】:Warning: control reaches end of non-void function - iPhone警告:控制到达非无效函数的结尾 - iPhone
【发布时间】:2011-01-27 06:35:27
【问题描述】:

我不断收到“警告:控制到达非无效函数的结尾”的代码:

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

我怎样才能摆脱这个警告?

谢谢。

【问题讨论】:

    标签: iphone objective-c xcode


    【解决方案1】:

    在方法末尾添加return 0;。如果没有满足if 条件,这基本上是一种故障保护。

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

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

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

    【讨论】:

    • +1,OP 的实际警告可能是“控制到达非 void 函数的末尾”。
    • 是的,基本上任何说它会返回一些东西的方法或函数都应该返回一些东西。由于无法保证满足任何if 条件,因此可能不会返回任何内容。
    • 非常感谢它运行良好,您的解释也非常有用。我真的很感谢你的时间:)
    【解决方案2】:

    一个选项:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-02
      • 1970-01-01
      • 2019-08-29
      • 2012-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多