單例是經(jīng)常使用的一種設(shè)計模式,簡便快捷南蹂。
1:使用性能較好的方式
+ (instancetype)shareInstance{
static Singleton* single;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
single = [[Singleton alloc] init];
});
return single;
}
2:不引用時可釋放的方式(使用 weak)
+ (id)sharedInstance
{
static __weak SingletonClass *instance;
SingletonClass *strongInstance = instance;
@synchronized(self) {
if (strongInstance == nil) {
strongInstance = [[[self class] alloc] init];
instance = strongInstance;
}
}
return strongInstance;
}