检查 Objective-C 阻止 class

Check Objective-C block class

我看过this,但没有找到解决方案。

我有一个计时器

self.checklinkTimer = [NSTimer scheduledTimerWithTimeInterval:5
                                                           target:self
                                                         selector:@selector(doWithHandler:)
                                                         userInfo:nil
                                                          repeats:YES];

和方法

-(void) doWithHandler:(void (^)(BOOL isValid))handler
{
    handler(isSessionValid);
}   

以及我使用 doWithHandler 方法中的布尔值的其他方法。

doWithHandler 中,我需要检查处理程序是否为 NSTimer class,在这种情况下,不要执行 handler(isSessionValid) .

我试过 if ( ! [(id)handler isKindOfClass:[NSTimer class]] )@try @catch 但这行不通。

问题:我如何检查块class?

你可以做类似的事情而不是你最初的想法来实现同样的行为:

self.checklinkTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];

那么你有:

- (void)timerFireMethod:(NSTimer *)timer {
    [self doWithHandler:nil];
}

其中

- (void)doWithHandler:(void (^)(BOOL isValid))handler {
    if (handler) handler(isSessionValid); // I don't know where `isSessionValid` comes from...
}