2013-03-23 61 views
0

數據時,我有以下代碼後5秒通過特定的ID:應用程序崩潰試圖通過在的NSTimer

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 
[dict setObject:[NSNumber numberWithInteger:currentID] forKey:@"ID"]; 
timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(checkID:) userInfo:dict repeats:NO]; 

- (void)checkID:(NSTimer*)t{ 
    NSInteger timerID = [[[t userInfo] objectForKey:@"ID"] integerValue]; 
    if (timerID == _myID){ 
     NSLog(@"got ID"); 
    } 
} 

但我的應用程序只是崩潰計時器結束後。有任何想法嗎?我在四處尋找幫助,但我只能找到上面的代碼,顯然它適用於其他人。
謝謝

+1

ARC或MRC?如果你不使用ARC,那麼你應該釋放字典。 – 2013-03-23 22:45:48

+0

'timer'的範圍是什麼?這裏很難說。 – 2013-03-23 22:46:34

+1

這段代碼沒有錯,它對我來說工作得很好。不用DigiMonk在他的回答中寫的名字是好的,但那不是解決你的問題的方法。你還改變了別的嗎? – rdelmar 2013-03-23 23:50:46

回答

0

我測試了你的代碼,它在這裏工作,但沒有「計時器」屬性。

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 
[dict setObject:[NSNumber numberWithInteger: currentID ] forKey:@"ID"]; 
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(checkID:) userInfo:dict repeats:NO]; 

我想你不需要在這種情況下的「計時器」屬性。

+0

艱難的答案已被接受,它爲OP工作,我認爲這可能會導致一個問題:如果自動釋放池被耗盡,定時器不再生存,並且該方法不會被執行。 – 2013-03-23 22:57:51

+2

運行循環保留其定時器,不要認爲會發生。這裏唯一的是我們沒有對我們可以使用的定時器的引用,但是定時器存在並且在觸發之前是有效的。 – 2013-03-23 23:22:29