【发布时间】:2009-12-11 20:57:34
【问题描述】:
我仍然对内存管理的微妙方面感到不安,并且我对我在一些示例代码中看到的激进的保留/释放有疑问。具体来说:
- (void)loadContentForVisibleCells
{
NSArray *cells = [self.tableView visibleCells];
[cells retain];
for (int i = 0; i < [cells count]; i++)
{
// Go through each cell in the array and call its loadContent method if it responds to it.
FlickrCell *flickrCell = (FlickrCell *)[[cells objectAtIndex: i] retain];
[flickrCell loadImage];
[flickrCell release];
flickrCell = nil;
}
[cells release];
}
为什么在 FlickrCell 上使用 [保留/释放] 循环(第 8 行和第 10 行)?该单元格位于一个 NSArray 中,根据定义,它保留了它的内容(我认为......?),并且 NSArray 本身被保留了。为什么需要这种额外的保留?
此外,为什么在 [self.tableView visibleCells](第 3 行)返回的 NSArray 上保留?数组在此方法调用期间是否保证存在?
非常感谢。
【问题讨论】: