views:

45

answers:

1

I'm enumerating a dictionary and checking for a known date. If the exact date (both date and time) matches my known date, I want the for loop to stop enumerating. THe issue I'm having is that during the check, I break out of the if statement instead of the for loop.

//known date has already been applied to self.knownDate
for (id object in array) {
    NSDate *objectDate = object.date;
    if ([self.knownDate isEqualToDate:objectDate]) {
        break;
    }
    NSLog(@"date didn't match, move on to next entry");
}

The problem is, the for loop keeps going, because only the IF statement is broken.

How can I refactor this so that the for loop quits?

By the way, I need to check both date & time from the NSDate object. The documentation says isEqualToDate checks sub-second differences, but it appears to be catching anything that is during the same day, not including time.

+2  A: 

There is no such thing as breaking out of an if, break inside the if should get you out of the for. Anyway, you can always try something like [self.knownDate isEqualToDate:objectDate && break instead, but that's not the issue you are having. To make sure, put some debug lines in there, even just a simple command to print "hey I'm here" in the if block.

Oblomov
Yep you're correct! I was stupidly restarting the loop later in the method.
JustinXXVII