sorry if the title is worded oddly, not really sure how to say it. I am adding contents of an array of object’s from a JSON into another array, so I can do use a sectioned UITableView. However, when I do the removeAllObjects function call when I’ve filled enough for the first section, add them to my array, and start to load up the second, it results in the entire first section being replaces by the second section. Is NSMutableArray at fault, or is it something I am not thinking of?
weekScheduleArray = [[NSMutableArray alloc] initWithCapacity:2];
NSMutableArray *temp = [[NSMutableArray alloc] init];
for (int count = 0; count < [jsonObject count]; count++) {
if (count < 7) {
[temp addObject:[jsonObject objectAtIndex:count]];
} else if (count == 7) {
[weekScheduleArray addObject:temp];
[temp removeAllObjects];
[temp addObject:[jsonObject objectAtIndex:count]];
} else {
[temp addObject:[jsonObject objectAtIndex:count]];
}
}
[weekScheduleArray addObject:temp];
Calling
[weekScheduleArray addObject: temp]does not add a copy oftemptoweekScheduleArray; it adds the array itself. Therefore when you subsequentlyremoveAllObjectsfromtempthen the array inweekScheduleArrayis also empty. You can avoid the issue with: