xcode 控制可能到达非空函数错误 if 语句的结尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19875161/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:16:33  来源:igfitidea点击:

Control may reach end of non-void function error if-statement

iosobjective-cxcodeuitableview

提问by tracifycray

I'm getting the error Control may reach end of non-void function on this code:

我在此代码上收到错误 Control may reach end of non-void function:

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

Why?

为什么?

回答by Midhun MP

Because when your all ifcondition fails, you are not returning anything from the function.

因为当你的所有if条件都失败时,你不会从函数中返回任何东西。

Also multiple return statement in a function is not a good practice.

此外,函数中的多个 return 语句也不是一个好习惯。

Do it like:

这样做:

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

回答by Gennadiy Ryabkin

If you think its ok disable it:

如果您认为它可以禁用它:

enter image description here

在此处输入图片说明

回答by T. Benjamin Larsen

Midhun MP has your answer and better code style. I would strongly advice replacing all those nested else-ifs with a switch-statement as, well you don't really want else-ifs if you can avoid them...

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

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

回答by akim

While I agree with most answers that suggest to avoid multiple returns in the general, on occasions multiple returns is nice and useful. For instance dispatching on an enum:

虽然我同意大多数建议return在一般情况下避免使用多个s 的答案,但有时多个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';
}

If you compile with GCC, you get (C or C++, it's the same):

如果你用 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 does not complain. Which is not so nice, actually, since it also compiles this without warnings:

Clang没有抱怨。实际上,这不是很好,因为它也编译没有警告:

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

which results in:

这导致:

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

So one has to "fight" GCC's warning. One wrong approach would be to add say

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

default:  abort();

to the switch. Sure, it cures the symptom, but now GCC will no longer complain if I add a new direction, say zenith, but forget to cover it in to_string. So really, never use a default case when switching on an enum.

switch. 当然,它可以治愈症状,但现在 GCC 不会再抱怨如果我添加一个新的direction,例如zenith,但忘记在to_string. 所以真的,在打开 enum 时永远不要使用默认情况

Then you can leave an abortafterthe switch(which is clumsy to do without using inner returns).

然后你就可以离开了abort之后switch(这是笨拙而不使用内做returnS)。

回答by Suhit Patil

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

or

或者

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