OC中线程安全的单例

 

@implementation MySingleton

+ (instancetype)sharedInstance
{
    static MySingleton* instance = nil;
    static dispatch_once_t  onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[MySingleton alloc]initPrivate];
    });
    return instance;
}

-(id)init
{
    @throw [NSException exceptionWithName:@"Singleton init" reason:@"use [MySingleton sharedInstance]" userInfo:nil];
}

- (instancetype)initPrivate
{
    self = [super init];
    if (self) {
        //do your init
    }
    return self;
}

@end

 

posted @ 2015-05-11 16:37  郭晓倩  阅读(335)  评论(0编辑  收藏  举报