- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在将一些代码转换为 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 做同样的事情?
最佳答案
使用 __autoreleasing
限定符声明 UIView *view
引用,如下所示:
- (UIView *)viewWithTag:(int)tag
{
__autoreleasing UIView *view;
__unsafe_unretained UIView *aView;
for (int i = 0; i < [self count]; i++)
{
aView = [self objectAtIndex:i];
if (aView.tag == tag)
{
view = aView;
//Since you declared "view" as __autoreleasing,
//the pre-ARC equivalent would be:
//view = [[aView retain] autorelease];
[self removeObjectAtIndex:i];
break;
}
}
return view;
}
__autoreleasing
将为您提供完全您想要的东西,因为在赋值时新指针被保留、自动释放,然后存储到左值中。
关于objective-c - removeObjectAtIndex 导致 "message sent to deallocated instance",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7912550/
这是我在 ViewController.m 文件中的代码, - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animat
从我的 NSMutableArray 中删除时出现以下错误 -[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to inst
这是我不断收到的错误: 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSArrayI removeObjectAtIndex:]:无法识别的
我有一个 NSMutableArray,我分配给它: NSMutableArray *newElements = [[NSMutableArray alloc] initWithObjects:sel
我有一个 TableView ,其中包含许多我希望能够删除的行。填充 TableView 的数据存储在 NSMutableArray 中。要删除一行中的数据,我调用: - (void)deleteFr
我正在使用下面的代码来检查一个对象是否存在,如果它存在则删除它: if(titlescopy.count >= i) { if([[titlescopy objectAtIndex:i] isK
如果我有一个包含 10 个对象的 NSMutableArray 我运行这行代码 [tempArray removeObjectAtIndex:0]; 然后 [tempArray count] 应该返回
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我有一个 UITableViewController,标题中有一个 NSMutableArray,如下所示: NSMutableArray *someArray; 我还声明了该属性: @propert
我正在尝试根据字符串的长度过滤掉一个字符串数组。总的来说,我对 Objective C 和 OOP 完全陌生。 wordList=[[stringFile componentsSeparatedByC
我正在尝试根据字符串的长度过滤掉一个字符串数组。总的来说,我对 Objective C 和 OOP 完全陌生。 wordList=[[stringFile componentsSeparatedByC
我有一个带有表格 View 的 View Controller ,您可以在其中删除单元格。我有另一个处理称为书签的类,该类称为 BookmarkHandler .有一些方法可以上传书签、获取整个书签数
我正在 SpriteKit 中进行测试和原型(prototype)制作,并拥有双击翻转的 Sprite 。我在 _theArray 中有 52 个 sprite,它包含当前在 View 中的 spri
我有一组用户,我试图将其转换为 NSMutableArray 以便能够使用 removeObjectAtIndex,但它不起作用。 我收到错误 Cast from '[ModelUser]?'如果我尝
在使用 NSMutableArray 时,我可以 addObject: 然后使用 removeObjectAtIndex:0 删除前面的对象。来自文档: To fill the gap, all el
我看过很多帖子,其中介绍了从数组中正确删除对象的各种方法,但我不确定哪种方法最适合我的实例。我正在从一个 plist 中加载一个字典,这个字典包含许多数组,这些数组包含另一个字典。所以我有 3 个存储
NSMutableArray 中的奇怪行为。 我已经创建了对象并填充了它。 NSMutableArray *array = [[NSMutableArray alloc]
我有一个简单的方法,可以将数组的第一个元素放在末尾,然后将所有元素向下移动一个索引: -(NSArray*)backUpNotes:(NSArray*)notes { NSMutableArr
我正在编写一个应用程序来显示来自门户的一些新闻。使用 JSON 文件从 Internet 获取新闻,然后使用 CoreData 模型将其存储到 NSMutableArray 中。显然,用户无法从 In
你好我是 ios 开发人员的新手。我想删除 tableview 中的行。我将实现代码但是当我滑动 tableview 时点击删除按钮而不是显示错误 [__NSCFArray removeObjectA
我是一名优秀的程序员,十分优秀!