单例继承

2017-12-21  本文已影响0人  MisYing
static char kInstanceKey;
+ (instancetype)sharedManager {
    id instance = objc_getAssociatedObject(self, &kInstanceKey);
    if (!instance) {
        instance = [[super allocWithZone:NULL] init];
        objc_setAssociatedObject(self, &kInstanceKey, instance, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return instance;
}

+ (id) allocWithZone:(struct _NSZone *)zone {
    return [self sharedManager] ;
}

- (id) copyWithZone:(struct _NSZone *)zone {
    return [[self class] sharedManager] ;
}

参考文章:http://blog.csdn.net/wangyanchang21/article/details/50996830

上一篇 下一篇

猜你喜欢

热点阅读