【发布时间】:2018-09-13 16:10:43
【问题描述】:
我正在尝试将一些在 Timer 中使用的 Swift 翻译成 Objective-C 中的新闻代码效果。
在 Swift 中,您可以执行以下操作:
func type(string: String) {
var wordArray = ["Sox Win", "Verlander To Start", "Race Tightens"] // an array of strings
var wordIndex = 0
Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { (timer) in
self.textview.text.append(wordsArray[wordIndex])
wordIndex += 1
if wordIndex == wordArray.count {
timer.invalidate()
}
}
}
}
但是,在 Objective-C 中,您通常会看到:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 1.0
target: self
selector:@selector(update:)
userInfo: nil repeats:YES];
-(void) update:(NSTimer*)timer
{
int i=0;i<4;i++ {
NSLog(@"another second");
}
timer.invalidate;
timer = nil;
}
使用处理程序,我无法弄清楚如何迭代数组中的单词而不反复创建显然不起作用的数组。
- (void)updateView:(NSTimer *)timer
{
NSArray*items =@[@"item1", @"item2", @"item3", @"item4", @"item5"];
for(int i=0;i<[items count];i++){
self.textView.text = [self.textView.text stringByAppendingString:items[i]];
if (i == [items count]) {
[self.timer invalidate];
self.timer = nil;
}
}
}
我应该对 userInfo 做些什么,或者我如何利用时间一次更新一个单词?提前感谢您的任何建议。
【问题讨论】:
-
它实际上使用了
scheduledTimerWithTimeInterval:repeats:block:(iOS10+),它有一个块,在 Swift 中被翻译为一个闭包。你真的不想使用相同的方法而使用@selector()吗? -
您将创建可以检索的属性(您的对象的@property 持有它),或者如您所说,使用带有
wordArray和wordIndex的userInfo 等。它很冗长。
标签: ios objective-c nstimer