美文网首页
Category 创建并懒加载一个实例对象

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

作者: 风___________ | 来源:发表于2018-07-03 20:02 被阅读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);
    }
    

    相关文章

      网友评论

          本文标题:Category 创建并懒加载一个实例对象

          本文链接:https://www.haomeiwen.com/subject/ejwjuftx.html