1. 程式人生 > >GCD建立單例的方法

GCD建立單例的方法

#import "Demo.h"

static Demo *instance = nil;
@implementation Demo
//GCD方法建立單例

+(id)getDemo{

    if (instance == nil) {

        static dispatch_once_t onceToken;
        
        dispatch_once(&onceToken, ^{
            instance = [super alloc];
            
            instance = [instance init];
            
        });
    }
    return instance;
}

+(id)allocWithZone:(struct _NSZone *)zone
{
    if (instance == nil) {
        
        static dispatch_once_t onceToken;
        
        dispatch_once(&onceToken, ^{
            instance = [super allocWithZone:zone];
            
        });
    }
    return instance;
}

-(id)copyWithZone:(struct _NSZone *)zone
{
    Demo *laoluo = [[Demo alloc]init];
    Demo.name = @"demo";
    
    return demo;
}

@end