I'm parsing through an NSDictionary
of json-encoded events and placing them into a two-dimensional NSMutableArray
based on their month -- for display in a sectioned table view.
Since I am adding items to an array and then placing that array in an array (event_container
) in a loop, event_container
shows the correct number of arrays, however, they all appear to be duplicates of the last iteration, so all of the contents of event_container
are the same array.
I believe this is because it's a pointer and/or not being released. I'm unsure of an appropriate way around this or possibly even a better solution. I'm using ARC.
int month = 0;
int current_month = 0;
int counter = 0;
event_container = [[NSMutableArray alloc] init];
temp_array = [[NSMutableArray alloc] init];
for (NSDictionary *result in results)
{
NCEvent *anEvent = [[NCEvent alloc] init];
anEvent.title = [result objectForKey:@"title"];
anEvent.startdate = [result objectForKey:@"startdate"];
anEvent.enddate = [result objectForKey:@"enddate"];
NSDateFormatter *importDate = [[NSDateFormatter alloc] init];
[importDate setDateFormat:@"yyyy-M-d H:m:ss"];
anEvent.dateStart = [importDate dateFromString:anEvent.startdate];
anEvent.dateEnd = [importDate dateFromString: anEvent.enddate];
NSDateFormatter *exportDate = [[NSDateFormatter alloc] init];
[exportDate setDateFormat:@"d"];
anEvent.text_date = [exportDate stringFromDate: anEvent.dateStart];
NSDateFormatter *exportMon = [[NSDateFormatter alloc] init];
[exportMon setDateFormat:@"MMM"];
anEvent.text_mon = [exportMon stringFromDate: anEvent.dateStart];
NSDateFormatter *monthInt = [[NSDateFormatter alloc] init];
[monthInt setDateFormat:@"M"];
month = [[monthInt stringFromDate: anEvent.dateStart] intValue];
if(counter == 1){ //first month
current_month = month;
NSLog(@"I'm the first month: %i", month);
[temp_array addObject:anEvent];
}
else if(month > current_month){ //new month
NSLog(@"This is a new month");
current_month = month;
//add the events array to events container and reset the events array
[self.event_container addObject: temp_array];
[temp_array removeAllObjects];
[temp_array addObject:anEvent];
}
else{
NSLog(@"Same Month"); //same month
[temp_array addObject:anEvent];
}
NSLog(@"Event month integer: %i", month);
anEvent = nil;
counter++;
}
Those arrays are declared as properties:
@property (nonatomic, retain) NSMutableArray *event_container;
@property (nonatomic, retain) NSMutableArray *temp_array;