缜密的创建一个单例

static OnlyOne *_instance = nil;

 

+ (instancetype)shareInstance

{

    static dispatch_once_t onceToken ;

    dispatch_once(&onceToken, ^{

        _instance = [[super allocWithZone:NULL] init] ;

    }) ;

    

    return _instance;

}

 

+ (id)allocWithZone:(struct _NSZone *)zone

{

    return [OnlyOne shareInstance];

}

 

- (id)copyWithZone:(struct _NSZone *)zone

{

    return [OnlyOne shareInstance];

}

 

#if ! __has_feature(objc_arc)

- (oneway void)release

{

    

}

 

- (id)autorelease

{

    return _instance;

}

 

- (NSUInteger)retainCount

{

    return 1;

}

#endif

posted on 2016-10-18 20:09  敏言慎行  阅读(191)  评论(0)    收藏  举报

导航