开发者

how to release appDelegate variables

So, for my program i am using 5 different views that all need to access and share the same data. when i firs开发者_JAVA技巧t 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?


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...


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜