真正的单例就应该这样写

@implementationfirstVc

//管方说这是由于历史原因还存在.参数管方建义传入null

+(instancetype)allocWithZone:(struct_NSZone*)zone{

return[superallocWithZone:zone];

}

+(instancetype)firstVC{

staticfirstVc* vc;

staticdispatch_once_toneToken;

dispatch_once(&oneToken, ^{

//vc = [[superalloc]init];

vc = [[firstVcallocWithZone:nil]init];

});

returnvc;

}

//重写alloc

+(instancetype)alloc{

return[firstVcfirstVC];

}

-(instancetype)init{

self= [superinit];

returnself;

}


//第二种写法

@implementationfirstVc

staticfirstVc*shareVc;

//管方说这是由于历史原因还存在.参数管方建义传入null

+(instancetype)allocWithZone:(struct_NSZone*)zone{

return[superallocWithZone:zone];

}

+(instancetype)firstVC{

//

//static firstVc * vc;

//static dispatch_once_t oneToken;

//

//dispatch_once(&oneToken, ^{

//vc = [[super alloc] init];

//});

//return vc;

if(!shareVc) {

shareVc= [[superalloc]init];

}

returnshareVc;

}

//重写alloc

+(instancetype)alloc{

return[firstVcfirstVC];

}

-(instancetype)init{

self= [superinit];

returnself;

}

你可能感兴趣的:(真正的单例就应该这样写)