Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用NSDate、NSTimer和NSRunLoop设置定时执行时,在操作完成之前,我不需要释放哪一个?_Iphone_Objective C_Nsrunloop - Fatal编程技术网

Iphone 使用NSDate、NSTimer和NSRunLoop设置定时执行时,在操作完成之前,我不需要释放哪一个?

Iphone 使用NSDate、NSTimer和NSRunLoop设置定时执行时,在操作完成之前,我不需要释放哪一个?,iphone,objective-c,nsrunloop,Iphone,Objective C,Nsrunloop,下面是代码中处理它的部分: NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:1.0+index]; NSTimer *timer = [[NSTimer alloc] initWithFireDate:fireDate interval:0.5

下面是代码中处理它的部分:

NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:1.0+index];
        NSTimer *timer = [[NSTimer alloc] initWithFireDate:fireDate
                                                    interval:0.5
                                                    target:self
                                                  selector:@selector(countedtargetMethodGlow:)
                                                  userInfo:nil
                                                   repeats:NO];

NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
[timer release];

但是它是在一个循环中,所以我将对这些进行一个buncha,我不知道我需要留下什么来避免触发混乱。

您添加到运行循环中的每个计时器对象都由运行循环保留,直到它失效为止,这有效地表明运行循环在需要计时器时拥有计时器。因此,您可以释放这些计时器中的任何一个,而不会影响它们在运行循环中的调度方式。如果您出于某种独立目的需要它们,您不应该释放它们,因此即使运行循环已经完成,它们也会保证在您身边。

因此,如果我将计时器重新分配给我想要完成的另一个进程,然后第一个进程释放计时器,这会不会破坏第二个进程?不会,如果您的编码遵循正常的Cocoa规则。每个对象都保留其实例变量,并在完成时释放它们。这样,保留计数将保持平衡。您可能缺少的是,只有当保留计数达到零时,才会解除分配对象。