GCD建立單例的方法
阿新 • • 發佈:2018-12-30
#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