//基于gcd的单例模式
static Type *_instances; + (id)allocWithZone:(NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken,^{ _instances = [super allocWithZone:zone]; }); return _instances; } //创建一个获取单例的方法 + (Type *)sharedInstances { if(_instances == nil){ _instances = [[self alloc] init]; } return _instances; }
并且所有读取单例的地方都要用@synchronized “加锁”
时间: 2025-01-31 03:59:35