我什么时候应该发布[[UIApplication sharedApplication]委托]对象? [英] When should I release [[UIApplication sharedApplication] delegate] object?

查看:93
本文介绍了我什么时候应该发布[[UIApplication sharedApplication]委托]对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中多次使用以下代码(尤其是管理NavigationController):

I'm using the following code many times in my app (especially to manage a NavigationController):

MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 

我什么时候发布?

Thx for help,

Thx for helping,

Stephane

推荐答案

不要。 从不发布您的应用程序委托 - 它由操作系统自动管理。

Don't. Never release your application delegate - it is managed automatically by the OS.

如果您查看应用程序的main.m文件,您将会看一些初始化代表你的应用程序的 UIApplication 实例的代码 - 它负责管理应用程序委托的生命周期,而不是你的责任。

If you look in your app's main.m file, you'll see some code that initializes an instance of UIApplication that represents your app - it is its responsibility to manage the application delegate's lifecycle, not your responsibility.

编辑,因为@Goz指出,你应该发布,如果在某些时候你保留它。但是,由于应用程序对象(因此,通过扩展它的委托)保证在应用程序的生命周期内保持不变(除非你搞乱它),所以简单地对代表进行任何内存管理,因为这样可以避免意外过度释放或其他相关问题。

EDIT as @Goz points out, you should release it if at some point you retain it. However, since the application object (and therefore, by extension its delegate) is guaranteed to remain in scope for the life of the app (unless you go messing with it), it's far better to simply not do any memory management on the delegate, as this avoids the possibility of accidental over-release or other related issues.

这篇关于我什么时候应该发布[[UIApplication sharedApplication]委托]对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆