【发布时间】:2012-05-01 03:32:57
【问题描述】:
我需要经常访问我的核心数据托管对象上下文,而不是每次在每个类中获取[[UIApplication sharedApplication] delegate] 的实例并将其存储在一个变量中,我想知道这样做是否可以:
@interface NSObject(DelegateExtension)
- (AppDelegate*)appDelegate;
@end
@implementation
NSObject(DelegateExtension)
- (AppDelegate*)appDelegate
{
return (AppDelegate*)[[UIApplication sharedApplication] delegate];
}
@end
所以我可以在我的代码中的任何地方执行self.appDelegate。
这样做有什么问题吗?这是不好的编程习惯吗?
【问题讨论】:
标签: objective-c ios cocoa-touch