开发者

change speed of an array loop? Objective-C

I have this code

NSArray *food = [NSArray arrayWithObjects:@"Apples:",@"bacon",@"corn",@"donuts",@"elfs",@"fidge",nil];

for(int i = 0; i<6; i++){
    NSLog(@"item at index %i is %@",i,[food objectAtIndex:i]);

}

and right now they are all printed to the console instantly. How can I make a variable to dec开发者_如何转开发rease or increase the speed they are logged? I'm new at objective-C so thanks a lot for your help! :)


NSArray *food = [NSArray arrayWithObjects:@"Apples:",@"bacon",@"corn",@"donuts",@"elfs",@"fidge",nil];

// the number of seconds to wait between printing each item
double secondsToSleep = 1.0;

for(int i = 0; i<6; i++){
    [NSThread sleepForTimeInterval:secondsToSleep];
    NSLog(@"item at index %i is %@",i,[food objectAtIndex:i]);
}


There's a sleepForTimeInterval: method on NSThread that might do what you're looking for. The documentation is here.

Edit: Sorry, for Objective-C newbies, you would just type something like this:

[NSThread sleepForTimeInterval:0.01];


See the sleep() function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜