最近面试的时候,面试官让我写下单例,我是如下写的
+ (Singleton *)sharedSingleton{
static Singleton *singleton = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
singleton = [[Singleton alloc]init];
});
return singleton;
}但是面试官看了看说如果这样写的话是保证了线程安全,但是如果要是直接 [[Singleton alloc]init] 的话还是不能保证该对象只被创建一次,他说需要重载下+new, +alloc,+allocWithZone:, -copyWithZone:, 以及 -mutableCopyWithZone这些方法,请问真的是这样吗?给我解答下吧
+ (Singleton *)sharedSingleton{
static Singleton *singleton = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
singleton = [[Singleton alloc]init];
});
return singleton;
}但是面试官看了看说如果这样写的话是保证了线程安全,但是如果要是直接 [[Singleton alloc]init] 的话还是不能保证该对象只被创建一次,他说需要重载下+new, +alloc,+allocWithZone:, -copyWithZone:, 以及 -mutableCopyWithZone这些方法,请问真的是这样吗?给我解答下吧
+(id)allocWithZone:(struct _NSZone *)zone
{
if (singleton == nil) {
singleton = [super allocWithZone:zone];
}
return singleton;
}
static SingleInstance *instance = nil;@implementation SingleInstance#if 0
//单例创建一:
+ (instancetype)sharedInstance
{
@synchronized(self)
{
if (!instance)
{
//不存在就创建当前单例对象
instance = [[self alloc] init];
}
return instance;
}
}
#endif//单例创建二:
+ (instancetype)sharedInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
}); return instance;
}//alloc会触发,防止通过alloc创建一个不同的实例
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [super allocWithZone:zone];
});
return instance;
}- (id)copyWithZone:(nullable NSZone *)zone
{
return self;
}- (id)mutableCopyWithZone:(nullable NSZone *)zone
{
return self;
}//手动内存管理下写这些- (instancetype)retain
{
return self;
}- (oneway void)release
{
}- (instancetype)autorelease
{
return self;
}- (NSUInteger)retainCount
{
return MAXFLOAT;
}- (void)dealloc
{
}@end