Objective-C - 我是否需要取消分配未使用的保留属性?

发布于 2024-10-04 15:10:40 字数 82 浏览 1 评论 0原文

如果我有一个想要重用的自定义视图控制器类,但在一个实例中使用时,它有一个在视图生命周期中实际未使用的保留属性,我是否需要在 dealloc 中释放它?

If I have a custom view controller class that I want to reuse but when used in one instance has a retained property that isn't actually used during the view's lifecycle, do I need to release it in dealloc?

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

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

发布评论

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

评论(3

|煩躁 2024-10-11 15:10:40

您应该释放在 dealloc 中分配或保留的任何对象。

如果它在界面构建器中被引用,您还需要在视图控制器的 viewDidUnload() 中释放并设置为 nil 以及在 dealloc 中释放。

You should release any objects that you alloc or retain in dealloc.

If it's referenced in interface builder, you'll also want to release and set to nil in the viewDidUnload() of your view controller as well as releasing in your dealloc.

攒一口袋星星 2024-10-11 15:10:40

发送消息到 nil 是可以的,所以无论如何你应该在 dealloc 中释放你的保留属性。如果该属性尚未被使用,它将为零,并且向它发送释放实际上是一个空操作。

It's OK to send messages to nil, so you should just release your retain properties in dealloc no matter what. If the property hasn't been used, it will be nil and sending release to it is effectively a no-op.

各空 2024-10-11 15:10:40

您不需要对保留的财产调用释放。 setter 方法的实现方式是,给它一个 null 值将为您释放它。在你的 dealloc 方法中只需将其设置为 nil :

self.someProperty = nil;

You don't need to call release on a retained property. The setter method is implemented such that giving it a null value will release it for you. In your dealloc method just set it to nil:

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