Cloud Stack Ninja

I'm trying to add a series of hour intervals to an NSMutableArray using the below code. I'm successfully able to add the times to self.dailyTimes inside the method (the first log returns with my array of hours), however when I log self.dailyTimes the second time outside of the method, the only time that shows up in the log is 12:00AM. Any idea why the full range of times isn't appearing when I log the array outside of the method?

ViewController.m

  -(void)viewDidLoad {
    
    [super viewDidLoad]
    
        
        NSString *startTime = @"12:00 AM";
        NSString *endTime = @"12:00 PM";
    
    
        NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
        [timeFormat setDateFormat:@"hh:mm a"];
        NSDate* fromTime = [timeFormat dateFromString:startTime];
        NSDate* toTime = [timeFormat dateFromString:endTime];
        NSDate *dateByAddingThirtyMinute ;
        NSTimeInterval timeinterval = [toTime timeIntervalSinceDate:fromTime];
        NSLog(@"time Int %f",timeinterval/3600);
        float numberOfIntervals = timeinterval/3600;
        NSLog(@"Start time %f",numberOfIntervals);
    
        for(int iCount = 0;iCount < numberOfIntervals*1 ;iCount ++)
        {
            dateByAddingThirtyMinute = [fromTime dateByAddingTimeInterval:3600];
            fromTime = dateByAddingThirtyMinute;
            NSString *formattedDateString;
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"hh:mm a"];
            formattedDateString = [dateFormatter stringFromDate:dateByAddingThirtyMinute];
            NSLog(@"Time after 60 min %@",formattedDateString);
            
            self.dailyTimes = [[NSMutableArray alloc] init];
            [self.dailyTimes addObject:[dateFormatter stringFromDate:dateByAddingThirtyMinute]];
            NSLog(@"First log: The daily times are %@",self.dailyTimes);
          
         
        }
        
        NSLog(@"Second log:The daily times are %@",self.dailyTimes);
    
        
          }


Read more here: https://stackoverflow.com/questions/64398747/obj-c-adding-interval-of-times-to-nsmutablearray

Content Attribution

This content was originally published by Brittany at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: