0

I'm trying to loop through my array (finalArray) using the below code:

AppDelegate.m

 NSUserDefaults *final = [NSUserDefaults standardUserDefaults];
         NSArray *finalArray = [final objectForKey:@"notifyTimes"];

      NSLog(@"Time from AppDelegate in Background %@", finalArray);
    
    for (int i = 0; i<finalArray.count; i++) {
        
        NSString *dateString = [finalArray objectAtIndex:i++];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MMM d yyyy h:mm a"];
    NSDate *dateFromString = [dateFormatter dateFromString:dateString];
    
    NSLog(@"Date according to app delegate frm string %@", dateFromString);

        UILocalNotification *notification = [[UILocalNotification alloc]init];
        notification.fireDate =  dateFromString;

        [notification setAlertBody:@"Reminder: You have an upcoming appointment!"];
   
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }

For some strange reason, when I log finalArray the first time (outside of my loop), the following data is returned, which is correct:

Time from AppDelegate in Background (
    "Apr 14 2021 12:04 PM",
    "Apr 17 2021 12:27 PM",
    "Apr 27 2021 12:28 PM",
    "Apr 14 2021 12:47 PM",
    "Apr 30 2021 1:45 PM",
    "Apr 27 2021 1:45 PM",
    "Apr 14 2021 12:03 PM",
    "Apr 30 2021 11:46 AM",
    "Apr 29 2021 11:20 AM",
    "Apr 14 2021 12:40 PM",
    "Apr 14 2021 12:56 PM",
    "Apr 14 2021 1:20 PM",
    "Apr 14 2021 1:08 PM",
    "Apr 14 2021 1:20 PM"
)

But when I loop through this same array and log the result, the loop is missing multiple dates from the above logged:

2021-04-14 13:21:04.485190-0700 [18644:1261621] Date according to app delegate frm string Wed Apr 14 12:04:00 2021
2021-04-14 13:21:04.493588-0700 [18644:1261621] Date according to app delegate frm string Tue Apr 27 12:28:00 2021
2021-04-14 13:21:04.494519-0700 [18644:1261621] Date according to app delegate frm string Fri Apr 30 13:45:00 2021
2021-04-14 13:21:04.495384-0700 [18644:1261621] Date according to app delegate frm string Wed Apr 14 12:03:00 2021
2021-04-14 13:21:04.496157-0700 [18644:1261621] Date according to app delegate frm string Thu Apr 29 11:20:00 2021
2021-04-14 13:21:04.496913-0700 [18644:1261621] Date according to app delegate frm string Wed Apr 14 12:56:00 2021
2021-04-14 13:21:04.497700-0700 [18644:1261621] Date according to app delegate frm string Wed Apr 14 13:08:00 2021

Any insight onto why this is happening?

1 Answer 1

1

You are incrementing i inside your loop on this line:

NSString *dateString = [finalArray objectAtIndex:i++];

You probably just want:

NSString *dateString = [finalArray objectAtIndex:i];

By incrementing inside the loop you were unintentionally skipping every other element.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahhh total oversight. THANK YOU. Haha that was driving me mental.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.