第一种方法:
static MainViewController *shareInstance;
+ (MainViewController *)sharedInstance
{
@synchronized (self) {
if (nil == shareInstance) {
shareInstance = [[super allocWithZone:NULL] init];
}
}
return shareInstance;
}
第二种方法:
+ (MainViewController *)sharedInstance
{
static MainViewController *sharedInstance = nil;
static dispatch_once_t onceToken; // 锁
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}