removeObjectAtIndex 导致“消息发送到已解除分配的实例”

发布于 2024-12-12 04:26:44 字数 776 浏览 0 评论 0原文

我正在将一些代码转换为 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 做同样的事情?

I am converting some code to ARC. The code searches for an element in an NSMutableArray, then finds, removes, and returns that element. The problem is that the element gets deallocated immediately upon "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;
}

When I run it, I get

*** -[UIView respondsToSelector:]: message sent to deallocated instance 0x87882f0

at the second log statement.

Pre-ARC, I was careful to retain the object before calling removeObjectAtIndex:, and then to autorelease it. How do I tell ARC to do the same thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十秒萌定你 2024-12-19 04:26:44

使用 __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 将为您提供准确 你想要什么,因为在分配时,新的指针被保留,自动释放,然后存储到左值中。

请参阅 ARC 参考

Declare the UIView *view reference with the __autoreleasing qualifier, like so:

- (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 will give you exactly what you want because on assignment the new pointee is retained, autoreleased, and then stored into the lvalue.

See the ARC reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文