Reputation: 8789
I've got an array of objects that I'm wanting to sort through according to a certain value of the object (i.e. self.example.value
). I've created multiple mutable arrays:
NSMutableArray *array1, *array2, *array3 = [[NSMutableArray alloc] initWithObjects: nil];
and then iterate through the original array using a for loop. If the object matches a condition (self.example.value == someValue
). I add the object to one of the new arrays created above. However, when I start to use the arrays later on I noticed that they were empty. Using the debugger I noticed the following:
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
So basically, every iteration of the for loop clears the arrays created above. As it progresses through the code, no matter if the 'if' statements are TRUE or not, the arrays are allocated, but not populated. What am I missing here?
Upvotes: 1
Views: 116
Reputation: 237110
You're only assigning an array to array3
, so the other two are either junk or nil depending on what kind of variables you're dealing with here. I think you want:
NSMutableArray *array1 = [[NSMutableArray alloc] init];
NSMutableArray *array2 = [[NSMutableArray alloc] init];
NSMutableArray *array3 = [[NSMutableArray alloc] init];
Upvotes: 5