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

2018-07-03  本文已影响15人  风___________

.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);
}
上一篇下一篇

猜你喜欢

热点阅读