网站普遍的创建单例类的方法有下面两种:
+ (instancetype)sharedManager {
static id _sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}
+ (instancetype)sharedManager {
static id _sharedInstance = nil;
@synchronized(self) {
if (_sharedInstance == nil)
_sharedInstance = [[self alloc] init];
}
return _sharedInstance;
}
但是该如何避免意外的用[[alloc] init]
创建呢?主要是发现网上找到的大多仅仅只有上面的代码,少有考虑被init
或者copy
的情况
http://www.jianshu.com/p/08b1...
看看這篇我的寫部落格.
又去stackovweflow找了下方法,我覺得既然是單例模式,呼叫者就應該嚴格按照單例的要求,透過統一的介面(這裡是sharedInstance)去創建單例,而不應該出現呼叫[[class alloc] init]也能成功創建單例的情況,如果出現[[class alloc] init]的情況,我覺得更應該讓Xcode給出警告不能用此方法
額外創建有很多,還有new方法也可以,把這些方面都重載一遍回傳 sharedManager 實例,或者直接拋出異常
覆蓋allocWithZone和copyWithZone方法。
因為透過alloc或copy還是new,都是透過呼叫allocWithzone和copyWithzone來分配空間的。
你可以把sharedManager 方法裡面的程式碼寫到這兩個方法裡面,就可以從根本實作了單例的情況
🎜🎜(instancetype)init {
@throw [NSException異常WithName:@"停用"原因:@"請使用init代替..." userInfo:nil];
return self;
}
這樣寫就可以了