2016-08-12 26 views
0

我看過this,但沒有找到解決方案。檢查Objective-C塊類

我有一個計時器

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

和方法

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

和其他方法,其中i使用布爾值從doWithHandler方法。

doWithHandler我需要檢查處理程序是NSTimer類,在這種情況下,不是handler(isSessionValid)

我試過if (! [(id)handler isKindOfClass:[NSTimer class]])@try @catch但是這種方式不行。

問題:我該如何檢查塊類?

+0

」但這不起作用「:請解釋。 –

+3

爲什麼這個參數永遠不是'NSTimer'? – Droppy

+0

文檔:「計時器將自身作爲參數傳遞,因此該方法將採用以下模式: - (void)timerFireMethod:(NSTimer *)timer」。 –

回答

0

你可以穿上類似的東西,而不是爲實現相同的行爲你最初的想法:

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... 
}