2015-12-30 24 views
3

在我的ARC iOS項目中使用goto時,出現了此編譯器錯誤。(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 
+0

看起來像你在for(;;)語句中初始化了一個局部變量,然後你通過goto離開了這個範圍:ARC不再能夠釋放變量。在obj-c中的 – Moonkid

+0

「goto」。我第二次看到它,首先是在一本書中。 – mkeremkeskin

+1

你可以發佈一些更多的代碼? – Fonix

回答

4

我終於想通了!其實,說警告清楚,「跳繞過保留變量初始化」因此,在接下來的一節

//在繼續下一章節我宣佈&初始化一些對象!

我的代碼/問題是基本相同c99 goto past initialization

解決方法很簡單,只是增加了一個{}塊吧,這裏提到Why can't variables be declared in a switch statement?

對於那些誰知道爲什麼我仍然需要跳轉,我覺得這個解釋是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

沒有轉到主線代碼是窩深處(當然我們也可以引入一個輔助函數來處理它)。