2012-10-30 32 views
0

我正在爲使用ARC開發iOS應用程序而加速。偶爾,一個簡單的舊c結構ole c - 陣列是我需要完成的工作。在ARC之前,我只需將free()添加到我的dealloc方法中。藉助ARC,不再需要dealloc。我可以添加一條ARC指令來告訴編譯器處理釋放我的c數組嗎?這裏自動引用計數(ARC)。 ARC可以處理一個簡單的ole c-array嗎?

每湯姆的回答是dealloc方法

// EIVertex 
struct EIVertex { 
    GLKVector3 p; 
    GLKVector3 n; 
    GLKVector3 barycentric; 
    GLKVector2 st; 
}; 

typedef struct EIVertex EIVertex; 

// ivar declaration 
EIVertex *_vertices; 

// malloc an array of EIVertex 
_vertices = (EIVertex *)malloc([_triangles count] * sizeof(EIVertex)); 

// Note lack of [super dealloc] 
- (void)dealloc{ 

    // ARC will not handle mem. management for plain ole c arrays. 
    free(_vertices); 
} 
+0

EIVertex是一個Objective-C類嗎?你不應該在Objective-C類中使用'sizeof',因爲類的大小可以在運行時改變。也許你想要一個指針數組。 – newacct

+0

@newacct,EIVertex是一個簡單的ole C結構。我只是更新了代碼片段以使其更清晰。對困惑感到抱歉。 – dugla

回答

3

您仍然可以重載的dealloc。唯一的是你不能明確地稱它。所以按照以前的方式編寫dealloc,但不要在其中調用[super dealloc]

+0

你應該展示一個適用於ARC的'-dealloc'的例子。 :) –

+0

@Tom,只是爲了澄清你說沒有ARC指令? – dugla

+0

@Tom,例如,如果我簡單地聲明我的ivar爲__strong,那麼ARC會爲我調用'free()'? – dugla

相關問題