2012-05-02 67 views
1

我已經得到了我想根據對象的一個​​特定值(即self.example.value)進行排序通過對象的數組。我已經創建了多個可變數組:NSMutableArray的是for循環的每次迭代後釋放?

NSMutableArray *array1, *array2, *array3 = [[NSMutableArray alloc] initWithObjects: nil];

,然後通過使用for循環原始數組迭代。如果對象符合條件(self.example.value == someValue)。我將這個對象添加到上面創建的一個新陣列中。但是,當我稍後開始使用數組時,我注意到它們是空的。使用調試器,我注意到以下幾點:

for (customClass *object in arrayOfObject){ //starting here the debugger has NONE of the arrays created above 

    if (object.value == someValue){//after performing this line, the debugger shows array1 in memory BUT nothing in it EVEN if the 'if' statement isn't TRUE. 
     [array1 addobject:object]; 
    } else if (object.value == someOtherValue){//after performing this line, the debugger shows array2 in memory BUT nothing in it EVEN if the 'if' statement isn't TRUE. 
     [array2 addobject:object]; 
    } //and so forth 

所以基本上,for循環的每次迭代清除上面創建的陣列。當它通過代碼進行時,無論'if'語句是否爲TRUE,數組都被分配,但未被填充。我在這裏錯過了什麼?

回答

5

你只分配一個數組array3,所以另外兩個是取決於你在這裏處理什麼樣的變量,無論是垃圾還是零。我認爲你想要:

NSMutableArray *array1 = [[NSMutableArray alloc] init]; 
NSMutableArray *array2 = [[NSMutableArray alloc] init]; 
NSMutableArray *array3 = [[NSMutableArray alloc] init]; 
+0

所以沒有「單線」初始化多個相似的對象? – KronoS

+0

@KronoS:沒有。您可以通過快捷鍵多*相同*變量,但有沒有簡單的方法,說:「所有這些變量應持有調用一個方法,每一次變量的結果」。 – Chuck

+0

即使你使用了'NSMutableArray'就像指定初始化更好:'[NSMutableArray的頁頭] initWithCapacity:0];' – Alladinian