2013-03-23 31 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