2

我在主队列中有一个 for 循环排队块,如下所示:

  for (int x=0; x<5; x++) {
    double delayInSeconds = x * .03;
      dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
      dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        ... code here ...
      }
  }

只有第一个块被执行。我已经验证循环工作正常,循环正确的次数并且没有引发异常或错误。

4

1 回答 1

6

使用此代码:

for (int x=0; x<5; x++) {
    double delayInSeconds = x * .03;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        NSLog(@"Blah!!");
    });
}

“Blah”被注销 5 次。我猜您发布的代码与您在应用程序中的代码不同?我必须添加 ); 到 dispatch_after 调用结束。

于 2012-06-01T23:03:32.907 回答