在 Xcode 中接收“控制到达非空函数结束”错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6645266/
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
Receiving "Control reaches end of non-void function" error message in Xcode
提问by JonnyPolo
When adding this code to a simple calculator program I receive the error message "Control reaches end of non-void function".
将此代码添加到简单的计算器程序时,我收到错误消息“控制到达非空函数的结尾”。
Now after reading some response I know that I should be adding a return call so that the program doesn't hang up in the instance that the accumulator is not set but I want to know where to place it or which would be the proper way to phrase this.
现在,在阅读了一些回复后,我知道我应该添加一个返回调用,以便程序不会在未设置累加器的情况下挂断,但我想知道将它放在哪里或者哪种方法是正确的这句话。
Thanks, any help would be appreciated. I can provide the full program code if necessary as well.
谢谢,任何帮助将不胜感激。如果需要,我也可以提供完整的程序代码。
-(double) changeSign
{
accumulator = -accumulator;
}
-(double) reciprocal
{
accumulator = 1/accumulator;
}
-(double) xSquared
{
accumulator = accumulator * accumulator;
}
回答by David Gelhar
Very simply, each of the methods you have here is declared to return a double
, but you never actually return anything.
很简单,您在此处拥有的每个方法都声明为返回 a double
,但您实际上从未返回任何内容。
You need to do something like:
您需要执行以下操作:
-(double) changeSign
{
accumulator = -accumulator;
return accumulator;
}
Or, alternatively, if you don't intend to return the new value of accumulator
, change the return type to void
:
或者,如果您不打算返回 的新值accumulator
,请将返回类型更改为void
:
-(void) changeSign
{
accumulator = -accumulator;
}
ps. the issue is not that the program will "hang up in the instance that the accumulator is not set". It's that you are saying "Here a method that returns a double
", but the code does not actually return anything. The error message means "I got to the end of this function that you promised would return something, but you seem to have forgotten".
附:问题不在于程序将“在未设置累加器的情况下挂断”。这是您说“这里有一个返回 a 的方法double
”,但代码实际上没有返回任何内容。错误消息的意思是“我到了这个函数的末尾,你承诺会返回一些东西,但你似乎忘记了”。
回答by PengOne
You need to return
a double
. The basic syntax of a method is
你需要return
一个double
. 方法的基本语法是
-(return type)methodName
If accumulator
is a declared property, then your functions can return nothing, i.e. void
, like this
如果accumulator
是声明的属性,则您的函数可以不返回任何内容,即void
,像这样
-(void) changeSign
{
accumulator = -accumulator;
}
So then a call such as [self changeSign]
will return nothing but will alter the value of accumlator
.
因此,诸如此类的调用[self changeSign]
将不返回任何内容,但会改变accumlator
.
Alternately, you can have a return type and return accumulator
by
或者,你可以有一个返回值类型和返回accumulator
由
-(double) changeSign
{
return -accumulator;
}
Then you can do something like:
然后你可以做这样的事情:
self.accumulator = [self changeSign];
I doubt the former is what your after, but hopefully it makes the syntax more clear.
我怀疑前者是你的追求,但希望它能让语法更清晰。
回答by jscs
Your code indicates that these methods will each return a double
:
您的代码表明这些方法将各自返回一个double
:
- (double) reciprocal
{ // ^^ Return type
...
but none of them include return
statements. You need to add those in order for values to be passed back to the caller:
但它们都没有包含return
语句。您需要添加这些值以便将值传递回调用者:
- (double) reciprocal
{
accumulator = 1 / accumulator;
return accumulator;
}
Then in the calling code you can assign the result of this method to a variable:
然后在调用代码中,您可以将此方法的结果分配给一个变量:
double foo = [someObject reciprocal];
Or, if you don't intend to pass a value back (perhaps you are simply updating some internal state), change the return type to void
.
或者,如果您不打算将值传回(也许您只是在更新某些内部状态),请将返回类型更改为void
.
- (void) reciprocal
{ // ^^ Indicates that there is no return value
...
回答by mavericks
The thing is, when we use return type other than "void" we should return something to the calling function for example:
问题是,当我们使用“void”以外的返回类型时,我们应该向调用函数返回一些东西,例如:
(returnType)methodName
{
---required code---
return something(as;-1/0/1/particular Value);
}
回答by Shreyansh Shah
If You have define as
如果你定义为
-(void)methodName{
//No need to return any thing.
}
if You have write any return type.
如果您编写了任何返回类型。
-(int)methodName{
return int
}
Example
例子
-(void)getData{
//Write any thing without return.
}
-(int)getData{
int total = a + b;
return total;
}
Or
或者
-(int)getData{
return a + b;}