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

你可能感兴趣的:(单例,ios开发,gcd)