【问题标题】:IOS/Objective-C: NSTimer without handler possible in objective-C?IOS/Objective-C:在 Objective-C 中可能没有处理程序的 NSTimer?
【发布时间】: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 持有它),或者如您所说,使用带有wordArraywordIndex 的userInfo 等。它很冗长。

标签: ios objective-c nstimer


【解决方案1】:

Objective-C 支持基于相同块的NSTimer 方法。你的 Swift 方法的翻译是:

- (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]
        });
    }
}

不需要计时器。

【讨论】:

    猜你喜欢
    • 2019-03-06
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 2012-01-13
    • 1970-01-01
    • 2013-10-17
    • 2014-12-05
    相关资源
    最近更新 更多