xcode 尝试退出方法,使用返回,但实际上不返回任何内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9245701/
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
Trying to exit a method, using return, but doesn't actually return anything
提问by user339946
I'm using return EXIT_SUCCESS
to exit a void function. However I get a flag that says my method should not return a value.
我正在使用return EXIT_SUCCESS
退出 void 函数。但是,我得到一个标志,表明我的方法不应返回值。
The code works fine, but is there a better way to exit a method by using a return or something else?
代码工作正常,但是有没有更好的方法通过使用 return 或其他方式退出方法?
回答by Daniel
To add to the other answers, you can always return from a void function with return;
要添加到其他答案中,您始终可以从 void 函数返回 return;
回答by Rayfleck
Redefine the method as
将方法重新定义为
-(int)myMethod;
Then you can return EXIT_SUCCESS.
然后你可以返回 EXIT_SUCCESS。
Is any code calling this method and expecting a value?
是否有任何代码调用此方法并期待一个值?
回答by fdh
I don't think you understand how functions work. When you specify the prototype asvoid
you are saying the function won't return anything. You are then contradicting the prototype by trying to return something. Change the prototype to -(int)function
to return EXIT_SUCCESS. If you do not want to return something, but want to still check if it was successful you could pass in a pointer to the function: -(void)functionReturnValue:int* returnValue
. Make the function set returnValue to EXIT_SUCCESS on successful completion and have the calling function check returnValue.
我认为您不了解函数的工作原理。当您指定原型时,void
函数不会返回任何内容。然后你试图返回一些东西来与原型相矛盾。将原型更改-(int)function
为返回 EXIT_SUCCESS。如果你不想返回的东西,但想还是检查,如果它是成功的,你可以传递一个函数指针:-(void)functionReturnValue:int* returnValue
。成功完成后,使函数将 returnValue 设置为 EXIT_SUCCESS,并让调用函数检查 returnValue。