iOS 单例模式范例

时间:2023-03-09 18:26:46
iOS 单例模式范例

The singleton pattern is useful for creating objects that are shared across the entire application,

such as an HTTP client or a notification manager,

or objects that may be expensive to create, such as formatters.

+ (instancetype)sharedInstance {
static id _sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[self alloc] init];
}); return _sharedInstance;
}