佚名通过本文主要向大家介绍了alloc init,berryalloc,alloc,cpu sr alloc,alloc chrdev region等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
问题: 如何避免通过[[alloc] init]创建iOS单例类
描述:
解决方案1:
描述:
网站普遍的创建单例类的方法有下面两种:
+ (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
的情况
解决方案1:
这样写就可以了
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;
}
解决方案2:(instancetype)init {
@throw [NSException exceptionWithName:@"Disable" reason:@"Please use init instead..." userInfo:nil];
return self;
}
覆盖allocWithZone和copyWithZone方法。
因为通过alloc或者copy还是new,都是通过调用allocWithzone和copyWithzone来分配空间的。
你可以把sharedManager 方法里面的代码写到这两个方法里面,就可以从根本实现了单例的情况
额外创建有很多,还有new方法也可以,把这些方面都重载一遍返回 sharedManager 实例,或者直接抛出异常
解决方案5:又去stackovweflow找了下方法,我觉得既然是单例模式,调用者就应该严格按照单例的要求,通过统一的接口(这里是sharedInstance)去创建单例,而不应该出现调用[[class alloc] init]也能成功创建单例的情况,如果出现[[class alloc] init]的情况,我觉得更应该让Xcode给出警告不能用此方法
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
解决方案6:http://www.jianshu.com/p/08b1...
看看这篇我的写博客.