iOS 单例对象的创建

static PSDrawerManager *psManager = nil;
+(PSDrawerManager *)shareManager{
if (psManager == nil) {
psManager = [[PSDrawerManager alloc] init];
}
return psManager;
}
+(id)allocWithZone:(struct _NSZone *)zone{
return [self shareManager];
}
使用GCD
static PSDrawerManager *psManager = nil;
+(PSDrawerManager *)shareManager{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
psManager = [[super allocWithZone:NULL] init];
});
return psManager;
}
+(id)allocWithZone:(struct _NSZone *)zone{
return [self shareManager];
}

你可能感兴趣的:(iOS 单例对象的创建)