iPhone-scheduledTimerWithTimeInterval是否保留userInfo参数?
在这段代码中,我有两个NSLog,都说dict的retain计数为1。 如果阵列中有许多对象,计时器可能会在很长时间内触发,我可以保留用户信息中给出的dict吗?因为我猜这是自动释放,而scheduledTimerWithTimeInterval似乎没有保留它 理论上? 实际上iPhone-scheduledTimerWithTimeInterval是否保留userInfo参数?,iphone,cocoa,memory-management,nstimer,retain,Iphone,Cocoa,Memory Management,Nstimer,Retain,在这段代码中,我有两个NSLog,都说dict的retain计数为1。 如果阵列中有许多对象,计时器可能会在很长时间内触发,我可以保留用户信息中给出的dict吗?因为我猜这是自动释放,而scheduledTimerWithTimeInterval似乎没有保留它 理论上? 实际上 - (void) doItWithDelay { NSArray* jobToDo = /* get an autorelease array */ NSTimeInterval nextLaunch
- (void) doItWithDelay
{
NSArray* jobToDo = /* get an autorelease array */
NSTimeInterval nextLaunch = 0.1;
int i=1;
for (NSDictionary* dict in jobToDo) {
NSLog(@"dict %d has %d retain count", i++, [dict retainCount]);
// HERE [dict retain] ???
[NSTimer scheduledTimerWithTimeInterval:nextLaunch target:self selector:@selector(doIt:) userInfo:dict repeats:NO];
nextLaunch += 1.0;
}
}
- (void) doIt:(NSTimer*)theTimer
{
NSDictionary* dict = [theTimer userInfo];
NSLog(@"dict has now %d retain count", [dict retainCount]);
// Do some stuff with dict
}
Apple NSTimer文档称将保留用户信息。下面是从文件中引用的 指定的对象由计时器保留,并在计时器无效时释放 你的第二个NSLog显示为1,所以我猜它已经被自动删除,但计时器仍然保留它
编辑:正如bbum所建议的,我们不应该依赖于保留计数。因此,我们很幸运,文档清楚地说明了这一点。正如霍姆桑所说,文档明确地说明对象被保留 故事结束了
retainCount
没用;别这么说。对象的绝对保留计数是一个实现细节。在尝试删除其用户信息之前,不要使NSTimer无效。对象的绝对保留计数没有意义。你的结论是错误的<代码>自动释放不会直接影响保留。