在for循环的每次迭代后,NSMutableArray都会被释放?

Jam*_*rtz 1 arrays memory-management objective-c nsmutablearray

我有一个对象数组,我想根据对象的某个值(即self.example.value)进行排序.我创建了多个可变数组:

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
Run Code Online (Sandbox Code Playgroud)

所以基本上,for循环的每次迭代都会清除上面创建的数组.随着代码的进展,无论'if'语句是否为TRUE,都会分配数组,但不会填充数组.我在这里错过了什么?

Chu*_*uck 5

你只是分配一个数组array3,所以其他两个是垃圾或零,这取决于你在这里处理的变量类型.我想你想要:

NSMutableArray *array1 = [[NSMutableArray alloc] init];
NSMutableArray *array2 = [[NSMutableArray alloc] init];
NSMutableArray *array3 = [[NSMutableArray alloc] init];
Run Code Online (Sandbox Code Playgroud)