(iOS) 使用goto时跳转绕过保留变量的初始化

(iOS) Jump bypasses initialization of retaining variable when using goto

我在我的 ARC iOS 项目中使用 goto 时遇到了这个编译器错误。

Cannot jump from this goto statement to its label. Jump bypasses initialization of retaining variable

我知道 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

我终于明白了!其实警告说的很清楚了,"Jump bypasses initialization of retaining variable"所以在下一节

//In proceed to next section I declare & init some object!

我的codes/problem和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? ,尤其是 "Cleanly exiting a function",请在此处查看示例 http://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c

without goto mainline代码嵌套很深(当然我们也可以引入辅助函数来处理)