简体   繁体   中英

For loop won't execute

The following code doesn't loop:

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"Inside method, before for loop");

    NSLog(@"dayBarArray.count = %lu", (unsigned long)dayBarArray.count);

    for (int i = 0; i < dayBarArray.count; i++) //*** I've tried it this way

//    for (DayChartBar *bar in dayBarArray) //*** I've tried it this way
    {
        NSLog(@"Inside for loop");

        DayChartBar *bar = [dayBarArray objectAtIndex:i];

        UIView *thisLabel = [self.scroller viewWithTag:(bar.tag + 100)];

        CGRect visibleRect;
        visibleRect.origin = self.scroller.frame.origin;
        visibleRect.size = self.scroller.bounds.size;

        NSLog(@"bar.frame = %@",NSStringFromCGRect(bar.frame));
        NSLog(@"visibleRect.frame = %@",NSStringFromCGRect(visibleRect));

        if (CGRectIntersectsRect(visibleRect,bar.frame))
        {
            NSLog(@"Inside if statement");

            CGRect intersection = CGRectIntersection(visibleRect,bar.frame);
            NSLog(@"bar.Frame is %@",NSStringFromCGRect(bar.frame));

            thisLabel.center = CGPointMake((thisLabel.center.x), CGRectGetMidY(intersection));
        }

        else
        {
            thisLabel.center = thisLabel.center;
            NSLog(@"Inside else statement");
        }
    }
    NSLog(@"Still inside method, after for loop");

}

Here are the results of the strategically placed NSLog statements:

2014-08-17 10:07:37.221 WMDGx[54442:90b] Inside method, before for loop
2014-08-17 10:07:37.222 WMDGx[54442:90b] dayBarArray.count = 0
2014-08-17 10:07:37.223 WMDGx[54442:90b] Still inside method, after for loop

As you can see, I've tried both a traditional for loop and fast enumeration. Neither will execute. As the NSLogs indicate, I enter the method, but skip the loop, regardless of the approach I've taken.

I'm pretty sure I've written good code since I've successfully used very similar syntax (both types) in many other places around this same app. And yes, I've looked at dozens of similar questions on SO, as well as consulting basic texts on Objective C for loops, but without resolution.

As always, I'm open to the possibility that I've made a stupid mistake, but, if so, it eludes me. The code inside the for loop (I'm trying to relocate a UILabel in response to the scrolling of an associated UIView ) is probably irrelevant, but I included it just in case.

All help and observations appreciated!

Edit:

As kindly pointed out below, the answer to this question is that the relevant dayBarArray is empty. I just checked the code that adds the objects, and am perplexed. I certainly should have seen the obvious answer in my NSLogs, and have no excuse except that I had checked the add code before and the bars appeared to be added. I'll report back when I've straightened out whatever is wrong there.

Second edit:

OK, it was a dumbass mistake on my part--I hadn't initialized the array. Just the same, had I not posted the question, and since I believed I had checked the contents of the array (actually, I was checking to see if tags had been applied to the UIViews), I don't know how long it would have taken me to realize what the logs were telling me. Thanks to everyone who replied!

dayBarArray.count is zero, and the condition in the loop is i < dayBarArray.count . Before entering the loop body, it tests 0 < 0 , which is false, so it never enters the body.

dayBarArray.count should return a number greater than 0, to be able for the loop to execute at least once. In you case, because (dayBarArray.count == 0), the condition-expression for the for loop:

i < dayBarArray.count

is false (because i == 0),

and the the execution of the program will not go inside the loop.

Your for loop will only execute when dayBarArray.count is greater than i and your log is telling you that dayBarArray.count equals 0 when i also equals 0.

Your code is working correctly. Check where you are adding items to dayBarArray.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM