手写单例

#import "NSObject.h"

 @interface JFAccountHelper : NSObject

+ (instancetype)sharedAccountHelper;

 @end

 

#import "JFAccountHelper.h"

@implementation JFAccountHelper

static JFAccountHelper *_accountHelper;

 

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

{

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        _accountHelper = [super allocWithZone:zone];

    });

    return _accountHelper;

}

 

+ (instancetype)sharedAccountHelper

{

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        _accountHelper = [[self alloc] init];

    });

    return _accountHelper;

}

 

- (id)copyWithZone:(NSZone *)zone

{

    return _accountHelper;

}

 @end

 

posted @ 2017-07-25 10:22  Huster2009  阅读(107)  评论(0编辑  收藏  举报