【发布时间】:2011-10-27 06:52:39
【问题描述】:
我正在将一些代码转换为 ARC。代码在 NSMutableArray 中搜索元素,然后查找、删除并返回该元素。问题是元素在“removeObjectAtIndex”后立即被释放:
- (UIView *)viewWithTag:(int)tag
{
UIView *view = nil;
for (int i = 0; i < [self count]; i++)
{
UIView *aView = [self objectAtIndex:i];
if (aView.tag == tag)
{
view = aView;
NSLog(@"%@",view); // 1 (view is good)
[self removeObjectAtIndex:i];
break;
}
}
NSLog(@"%@",view); // 2 (view has been deallocated)
return view;
}
当我运行它时,我得到了
*** -[UIView respondsToSelector:]: message sent to deallocated instance 0x87882f0
在第二条日志语句中。
在 ARC 之前,我在调用 removeObjectAtIndex: 之前小心地保留了对象,然后自动释放它。我如何告诉 ARC 做同样的事情?
【问题讨论】:
-
[self removeObjectAtIndex:i];是做什么的?
标签: objective-c ios nsmutablearray automatic-ref-counting