如何释放appDelegate变量

发布于 2024-12-08 08:27:21 字数 757 浏览 0 评论 0原文

因此,对于我的程序,我使用 5 个不同的视图,它们都需要访问和共享相同的数据。当我第一次开始为 iPhone 开发时,我找到了一种在 appDelegate 中创建和存储数据的方法。现在,我有大量可以从那里访问的变量。

我现在的问题是,如何对它们进行内存管理?

appDelegate.h

@property (nonatomic, retain) NSString *analysisModeForSave;
@property (nonatomic, retain) NSString *pdfPath;
@property (nonatomic, retain) NSString *state;
@property (nonatomic, retain) NSNumber *userLevel;
@property (nonatomic, retain) NSNumber *currentHiliteID;

然后在 .m 文件中 @synthesize 它们

并用于

Agri_ImaGIS_iPhoneAppDelegate *dataCenter = (Agri_ImaGIS_iPhoneAppDelegate *) [[UIApplication sharedApplication] delegate];

在函数中访问它们。现在,如果我不再需要该变量,我只需将其设置为 nil 即可。我应该在 appDelegate 的 dealloc 中释放它们吗?除了应用程序终止之外,内存是否会被清除?

So, for my program i am using 5 different views that all need to access and share the same data. when i first started developing for iPhone, i found a way to create and store the data in the appDelegate. now, i have a large amount of variables that i access from there.

my question now, is how do memory management of them?

appDelegate.h

@property (nonatomic, retain) NSString *analysisModeForSave;
@property (nonatomic, retain) NSString *pdfPath;
@property (nonatomic, retain) NSString *state;
@property (nonatomic, retain) NSNumber *userLevel;
@property (nonatomic, retain) NSNumber *currentHiliteID;

then @synthesize them in the .m file

and use

Agri_ImaGIS_iPhoneAppDelegate *dataCenter = (Agri_ImaGIS_iPhoneAppDelegate *) [[UIApplication sharedApplication] delegate];

to access them all in the function. right now if i don't need the variable anymore, i just set it to nil. should i release them in the appDelegate's dealloc? does the memory ever get cleared other then app termination?

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

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

发布评论

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

评论(2

我ぃ本無心為│何有愛 2024-12-15 08:27:21

虽然 Agri_ImaGIS_iPhoneAppDelegate 的单例实例在应用程序完成之前不会被释放,但在任何类的 dealloc 方法中释放保留属性是一个很好的做法。在这种情况下,系统无论如何都会在你之后清理干净,但会因为你的凌乱和行为不当而对你皱眉......

Although it is true that the singleton instance of Agri_ImaGIS_iPhoneAppDelegate will not be deallocated until the app finishes it is good practice to release retain properties in the dealloc method of any class. The system would clean up after you anyway in this case but would frown at you for being messy and badly behaved...

∞琼窗梦回ˉ 2024-12-15 08:27:21

一般规则听起来:
每当你分配一个对象时,你都会在同一个类中释放它。
这些 NSNumber 和 NSString 没有什么不同。
你可以在任何你喜欢的地方将它们设置为 nil,但你仍然必须在 dealloc 中释放它们。

The general rule sounds:
Whenever you alloc an object, you dealloc it in the same class.
These NSNumbers and NSStrings are no different.
You can set them to nil whereever you like but you still have to release them in the dealloc.

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