网站普遍的创建单例类的方法有下面两种:
+ (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
的情况
黄舟2017-04-18 09:43:01
又去stackovweflow找了下方法,我覺得既然是單例模式,呼叫者就應該嚴格按照單例的要求,透過統一的介面(這裡是sharedInstance)去創建單例,而不應該出現呼叫[[class alloc] init]也能成功創建單例的情況,如果出現[[class alloc] init]的情況,我覺得更應該讓Xcode給出警告不能用此方法
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
迷茫2017-04-18 09:43:01
覆蓋allocWithZone和copyWithZone方法。
因為透過alloc或copy還是new,都是透過呼叫allocWithzone和copyWithzone來分配空間的。
你可以把sharedManager 方法裡面的程式碼寫到這兩個方法裡面,就可以從根本實作了單例的情況
大家讲道理2017-04-18 09:43:01
(instancetype)init {
@throw [NSException異常WithName:@"停用"原因:@"請使用init代替..." userInfo:nil];
return self;
}
天蓬老师2017-04-18 09:43:01
這樣寫就可以了
static Singleton *slt = nil;
+ (instancetype)sharedInstance{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
slt = [[self alloc]init];
});
return slt;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
slt = [super allocWithZone:zone];
});
return slt;
}
- (id)copyWithZone:(NSZone *)zone
{
return slt;
}