【转载XMG】单例模式标准代码
2016-12-06 本文已影响15人
陈水寒
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;
}