Category 创建并懒加载一个实例对象

.h

@property (nonatomic, strong)CFNotificationComponent *notificationComponent;

.m

// 通知懒加载初始化
static void *NotificationComponentKey = &NotificationComponentKey;
- (CFNotificationComponent *)notificationComponent
{
    id component = objc_getAssociatedObject(self, NotificationComponentKey);
    // lazily create the KVOController
    if (nil == component) {
        component = [[CFNotificationComponent alloc] init];
        self.notificationComponent = component;
    }
    
    return component;
}
- (void)setNotificationComponent:(CFNotificationComponent *)notificationComponent
{
    objc_setAssociatedObject(self, NotificationComponentKey, notificationComponent, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Map 是一种很常见的数据结构,用于存储一些无序的键值对。在主流的编程语言中,默认就自带它的实现。C、C++ 中的...
    一缕殇流化隐半边冰霜阅读 13,108评论 23 67
  • 立行!不拖不摸 做事不拖摸。。。不要忌讳太多,不要老想些有的无的,已经浪费掉太多时间...
    立行阅读 1,562评论 0 1
  • 现在社会什么都讲究一个“争”字,与父母之间争道理,与客户之间争小利,与朋友之间争风头,就连过个马路都要争一争谁先走...
    DaYby阅读 3,318评论 0 0
  • 翻查汉语字典,说“小”字是指面积、体积、容量、数量、强度、力量不及一般或不及所比较的对象,与“大”相对;“...
    ed1b3fd43407阅读 2,795评论 0 1

友情链接更多精彩内容