本文介绍了UIViewController的didReceiveMemoryWarning在ARC环境中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在评估切换到ARC(自动引用计数)和重构以应用于我的代码。
在我需要弄明白的事情中,有:

I'm evaluating the switch to ARC (automatic reference counting) and the refactoring to apply to my code.Among the things I have to figure out, there is this:

didReceiveMemoryWarning ARC不允许对象?
目前,我使用该方法释放属于我的控制器的对象,并且可以通过延迟getter方便地检索:

what should I do in didReceiveMemoryWarning if the explicit release of objects is not allowed by ARC?Currently, I use that method to release objects belonging to my controller and that are easily retrievable via lazy getters:

- (void)didReceiveMemoryWarning {
    [_foo release]; _foo = nil;
    [super didReceiveMemoryWarning];
}

和相对延迟getter:

and the relative lazy getter:

- (Foo *)foo {
    if (_foo) {
        return _foo;
    }
    return (_foo = [[Foo alloc] init]);
}

似乎不可能在ARC中实现这样的模式我做?

It seems impossible to implement such "pattern" in ARC… so, what should I do? Should didReceiveMemoryWarning be considered "deprecated"/useless in ARC?

推荐答案

ARC处理保留和释放代码,因此将_foo设置为nil足以允许ARC生成的代码执行释放。

ARC handles the retain and release code so setting _foo to be nil is sufficient to allow the ARC generated code to perform the release. You don't explicitly release, you simply manage your object graph and the ARC generated code will perform the release when appropriate.

阅读Apple的文档以获取更多信息。

Read Apple's Transitioning To ARC Release Notes document for more information.

这篇关于UIViewController的didReceiveMemoryWarning在ARC环境中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 00:45