【问题标题】:For loop won't executeFor循环不会执行
【发布时间】:2014-08-17 18:06:01
【问题描述】:

以下代码不会循环:

-(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");

}

以下是策略性放置的NSLog 语句的结果:

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

如您所见,我尝试了传统的for loop 和快速枚举。两者都不会执行。正如NSLogs 所指出的,我进入了该方法,但跳过了循环,而不管我采用了什么方法。

我很确定我已经编写了很好的代码,因为我已经在同一个应用程序的许多其他地方成功地使用了非常相似的语法(两种类型)。是的,我已经查看了几十个关于 SO 的类似问题,以及关于 Objective C for 循环的基本文本,但没有解决方案。

与往常一样,我对我犯了一个愚蠢的错误的可能性持开放态度,但如果是这样,我会逃避。 for 循环中的代码(我正在尝试重新定位 UILabel 以响应关联 UIView 的滚动)可能无关紧要,但我将其包括在内以防万一。

感谢所有帮助和意见!

编辑:

正如下面所指出的,这个问题的答案是相关的 dayBarArray 是空的。我刚刚检查了添加对象的代码,很困惑。我当然应该在我的 NSLogs 中看到明显的答案,并且除了我之前检查过添加代码并且似乎添加了条之外没有任何借口。当我纠正那里的任何问题时,我会报告。

第二次修改:

好的,这是我的一个愚蠢的错误——我没有初始化数组。同样,如果我没有发布问题,并且由于我相信我已经检查了数组的内容(实际上,我正在检查标签是否已应用于 UIViews),我不知道它会持续多久让我意识到日志告诉我的是什么。感谢所有回复的人!

【问题讨论】:

  • 语法很火,集合只是空的。
  • 当您清楚地记录数组为空时,为什么还要执行循环?
  • 鸡蛋在我脸上!请参阅上面的编辑。谢谢!

标签: ios objective-c for-loop fast-enumeration


【解决方案1】:

dayBarArray.count 为零,循环中的条件为i &lt; dayBarArray.count。在进入循环体之前,它会测试0 &lt; 0,这是错误的,所以它永远不会进入循环体。

【讨论】:

    【解决方案2】:

    dayBarArray.count 应该返回一个大于 0 的数字,以便循环至少执行一次。在您的情况下,因为 (dayBarArray.count == 0),for 循环的条件表达式:

    i < dayBarArray.count
    

    为假(因为 i == 0),

    并且程序的执行不会进入循环。

    【讨论】:

      【解决方案3】:

      只有当 dayBarArray.count 大于 i 并且您的日志告诉您 dayBarArray.count 等于 0 而 i 也等于 0 时才会执行您的 for 循环。

      您的代码运行正常。检查您将项目添加到 dayBarArray 的位置。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-09
        • 1970-01-01
        • 2021-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多