【发布时间】:2011-07-30 23:55:05
【问题描述】:
在那段代码中,我有两个 NSLog 都说 dict 的保留计数为 1。 由于如果数组中有很多对象,可以长时间触发计时器,我可以保留用户信息中给出的字典吗?因为我猜是autorelease,而且scheduledTimerWithTimeInterval 好像没有保留。
理论上? 实用吗?
- (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
}
【问题讨论】:
标签: iphone cocoa memory-management nstimer retain