0

我正在寻找一种使用同一段代码运行代码 X 次的方法。

假设我想重复代码:

NSLog(@"repeat");

numberX 次:

int numberX;

无需根据 numberX 的值更改任何代码。

这将如何完成?如果您需要更多解释或解释我试图通过代码实现的目标,请发表评论。

4

4 回答 4

2

用户 for 循环:

for(int x = 0; i<NUMBER_OF_TIMES; x++){
  NSLog(@"repeat");
}

确保你阅读了这个 Objective-C Primer

于 2012-05-18T03:12:33.833 回答
0

您可以使用简单的 for 循环

for(int i = 0; i< numberX; i++) {
// the code you want to repeat

}
于 2012-05-18T03:09:09.030 回答
0

使用 for 循环!创建你的方法:

(void) aMethod{
NSLog(@"Repeat");
}

在 for 循环中使用原始方法中的方法:

int numberX = 123456789;
for(int loopInt = numberX; loopInt >= 0){
aMethod;
}
于 2012-05-18T03:11:26.317 回答
0

假设您要同步运行任务,您可以将任务插入串行调度队列。它是一个先进先出。

于 2012-05-18T04:04:42.187 回答