Objective-C支持相同的基于块的
NSTimer
- (void)type:(NSString *)string {
NSArray *wordArray = @[ @"Sox Win", @"Verlander To Start", @"Race Tightens" ];
__block NSInteger wordIndex = 0;
[NSTimer scheduledTimerWithTimeInterval:0.1 repeats:YES block:^(NSTimer * _Nonnull timer) {
// append wordsArray[wordIndex]
wordIndex += 1;
if (wordIndex == wordArray.count) {
[timer invalidate];
}
}];
}
dispatch_after
:
- (void)type:(NSString *)string {
NSArray *wordArray = @[ @"Sox Win", @"Verlander To Start", @"Race Tightens" ];
for (NSInteger i = 0; i < wordArray.count; i++) {
dispatch_after(i + 0.1, dispatch_get_main_queue(), ^{
// append wordsArray[i]
});
}
}