I need access to my core data managed object context often, and instead of getting an instance of the [[UIApplication sharedApplication] delegate] and storing it in a variable every time in every class, I was wondering if it would be ok to do this:
@interface NSObject(DelegateExtension)
- (AppDelegate*)appDelegate;
@end
@implementation
NSObject(DelegateExtension)
- (AppDelegate*)appDelegate
{
return (AppDelegate*)[[UIApplication sharedApplication] delegate];
}
@end
so then I can just do self.appDelegate anywhere in my code.
Is anything wrong with doing this that might night be obvious? Is it bad programming practice?
I usually use a global variable to point to the app delegate.
In MyAppDelegate.h:
In MyAppDelegate.m:
Now anyone who imports “MyAppDelegate.h” can use the
AppDelegatevariable to access your app delegate.