【发布时间】:2012-07-13 21:36:08
【问题描述】:
我正在尝试让 dealloc 方法在一些存储在/曾经存储在可变数组中的项目上运行,但似乎无法找到实现它的方法。
我在一个更大的 ARC 项目中工作时遇到了这个问题,并在这篇文章中找到了答案:dealloc method is not invoked when set an object to nil。在阅读了那个答案后,我觉得我理解了 ARC 应该如何处理有问题的代码(如下),但是在一个非常简单的测试项目中运行它之后,我得到了相同的结果。
在主视图控制器中,我初始化了一个可变数组(一个强属性)并向其中添加了一些其他视图控制器。然后我删除所有对象:
- (void)viewDidLoad{
[super viewDidLoad];
containerArray = [[NSMutableArray alloc]init];
for(int i = 0; i < 10; i++){
//item +1 (item at +1)
Item *item = [[Item alloc]initWithNibName:nil bundle:nil];
//item +1 (item at +2)
[containerArray addObject:item];
//ARC should release item -1 (item at +1...I think)
}
//removeAllObjects should release each item -1 (item(s) at 0)
[containerArray removeAllObjects];
//dealloc should be called...
}
在项目视图控制器中:
-(void)dealloc{
NSLog(@"item dealloc");
}
非常感谢任何帮助。
【问题讨论】:
标签: objective-c ios nsmutablearray automatic-ref-counting