【转载XMG】单例模式标准代码

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

+ (instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instansce = [[self alloc] init];
    });
    return _instansce;
}

- (id)copyWithZone:(NSZone *)zone
{
    return _instansce;
} 

推荐阅读更多精彩内容