#import "Singleton.h"
@implementation Singleton
static id manager = nil;
+ (Singleton *)manager {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[Singleton alloc] init];
});
return manager;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [super allocWithZone:zone];
});
return manager;
}
- (id)copy {
return [Singleton manager];
}
下面是本人所犯的錯(cuò)誤
#import "Singleton.h"
@implementation Singleton
static id manager = nil;
+ (Singleton *)manager {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[Singleton alloc] init];
});
return manager;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
return [Singleton manager]; // 創(chuàng)建對象init的時(shí)候會調(diào)用該方法,所以這種寫法的后果就是進(jìn)入死循環(huán)只恨。無限執(zhí)行manager方法和本方法。
}
- (id)copy {
return [Singleton manager];
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者