@implementation MySingleton + (instancetype)sharedInstance { static MySingleton* instance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [[MySingleton alloc]initPrivate]; }); return instance; } -(id)init { @throw [NSException exceptionWithName:@"Singleton init" reason:@"use [MySingleton sharedInstance]" userInfo:nil]; } - (instancetype)initPrivate { self = [super init]; if (self) { //do your init } return self; } @end
时间: 2024-10-07 20:40:47