1

我有一个对象数组,我想根据对象的某个值(即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

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

4

1 回答 1

5

您只是将一个数组分配给array3,因此其他两个是垃圾或 nil 取决于您在此处处理的变量类型。我想你想要:

NSMutableArray *array1 = [[NSMutableArray alloc] init];
NSMutableArray *array2 = [[NSMutableArray alloc] init];
NSMutableArray *array3 = [[NSMutableArray alloc] init];
于 2012-05-02T07:26:06.523 回答