【问题标题】:Control may reach end of non-void function error if-statement控制可能到达非空函数错误 if 语句的结尾
【发布时间】:2013-11-21 09:55:29
【问题描述】:

我收到错误 Control may reach end of non-void function on this code:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (changeData.selectedSegmentIndex == 0) {
        return self.tweets.count;
    } else if (changeData.selectedSegmentIndex == 1) {
        return self.tweets1.count;
    } else if (changeData.selectedSegmentIndex == 2) {
        return self.tweets2.count;
    }
}

为什么?

【问题讨论】:

  • 因为编译器没有你聪明!
  • 您已经在下面得到了答案(并且 switch 案例是您的方法的更好替代方案)但是因为这是关于没有最后一个 else 的 if-else if,我只是建议在最后一个 } 之前添加声明 else { return 0;} 或只是 return 0;

标签: ios objective-c xcode uitableview


【解决方案1】:

如果您认为可以禁用它:

【讨论】:

    【解决方案2】:

    虽然我同意大多数建议在一般情况下避免使用多个returns 的答案,但有时多个returns 很好且有用。例如在enum 上调度:

    #include <iostream>
    #include <string>
    
    enum direction { north, east, south, west };
    
    std::string to_string(direction d)
    {
      switch (d)
        {
    #define CASE(C) case C: return #C
          CASE(north);
          CASE(east);
          CASE(south);
          CASE(west);
    #undef CASE
        }
    }
    
    int main()
    {
      std::cout << to_string(west) << '\n';
    }
    

    如果你用 GCC 编译,你会得到(C 或 C++,都是一样的):

    $ g++-4.9 -Wall foo.cc
    foo.cc: In function 'std::string to_string(direction)':
    foo.cc:17:1: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^
    

    Clang 没有抱怨。实际上,这不太好,因为它也可以在没有警告的情况下编译它:

    int main()
    {
      std::cout << to_string(direction(666)) << '\n';
    }
    

    导致:

    $ clang++-3.5 -Wall foo.cc
    $ ./a.out
    zsh: illegal hardware instruction  ./a.out
    

    因此必须“对抗” GCC 的警告。一种错误的方法是添加say

    default:  abort();
    

    switch。当然,它治愈了症状,但是现在如果我添加一个新的direction,比如zenith,GCC 将不再抱怨,但忘记在to_string 中覆盖它。所以真的,在打开枚举时永远不要使用默认情况

    然后你可以在switch 之后留下一个abort (如果不使用内部returns 这样做会很笨拙)。

    【讨论】:

      【解决方案3】:
      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
      {
          NSInteger count = 0;
          if (changeData.selectedSegmentIndex == 0) {
              count = self.tweets.count;
          } else if (changeData.selectedSegmentIndex == 1) {
              count = self.tweets1.count;
          } else {
              count = self.tweets2.count;
          }
         return count;
      }
      

      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
         {
               NSInteger count = 0;
           if (changeData.selectedSegmentIndex == 0) {
              count = self.tweets.count;
           } else if (changeData.selectedSegmentIndex == 1) {
              count = self.tweets1.count;
           } 
           else if (changeData.selectedSegmentIndex == 2){
                  count =  self.tweets2.count;
           }
           return count;
         }
      

      【讨论】:

        【解决方案4】:

        Midhun MP 有您的答案和更好的代码风格。我强烈建议用 switch 语句替换所有嵌套的 else-if,因为如果你可以避免它们,你真的不想要 else-if...

        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
               NSInteger count = 0;
               switch (changeData.selectedSegmentIndex) 
                    {
                   case 0:
                       count = self.tweets.count;
                       break;
                   case 1:
                       count = self.tweets1.count;
                       break;
                   case 2:
                       count = self.tweets2.count;
                       break;
                   default:
                       break;
                    }
            return count;
        }
        

        【讨论】:

          【解决方案5】:

          因为当您的 all if 条件失败时,您不会从函数返回任何内容。

          在一个函数中使用多个 return 语句也不是一个好习惯。

          这样做:

          - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
          {
              int count = 0;
              if (changeData.selectedSegmentIndex == 0)
              {
                  count = self.tweets.count;
              }
              elset if (changeData.selectedSegmentIndex == 1)
              {
                  count  = self.tweets1.count;
              }
              else if (changeData.selectedSegmentIndex == 2)
              {
                  count  = self.tweets2.count;
              }
              return count;
          }
          

          【讨论】:

          • 好的,那我应该返回什么?
          • 另外,添加NSAssert 断言是有意义的,这样在开发构建中,如果索引具有意外值,则会引发异常。
          猜你喜欢
          • 1970-01-01
          • 2013-10-06
          • 1970-01-01
          • 1970-01-01
          • 2014-05-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多