在ARC iOS项目中使用goto时出现此编译器错误。

无法从此goto语句跳转到其标签。跳过旁路
初始化保留变量

我知道goto通常不好,但是...请告诉我如何解决。代码如下

//some process
NSArray *current = ... ;
if (current.count ==0) goto cleanup;
//proceed to next
if (processed failed) goto cleanup;
//further process

cleanup:
//clean up codes

最佳答案

我终于想通了!实际上,警告明确指出:“跳转绕过保留变量的初始化”,因此在下一部分中

//在下一节中,我声明并初始化一些对象!

我的代码/问题与c99 goto past initialization基本相同

解决方案很简单,只需在其中添加一个{}块即可,如这里提到的Why can't variables be declared in a switch statement?

对于那些想知道为什么我仍然需要goto的人,我认为这可以解释Is it ever advantageous to use 'goto' in a language that supports loops and functions? If so, why?,尤其是“干净地退出函数”,请查看此处的示例
http://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c

如果没有goto,主线代码会嵌套在嵌套条件的内部(当然,我们也可以引入一个辅助函数来处理它)。

07-24 09:51
查看更多