OC自定义单例
自定义一个MeloPerson对象,使之成为单例。
// 重写allocWithZone:方法,保证只会分配一次内存空间
+ (id)allocWithZone:(struct _NSZone *)zone
{
static MeloPerson *instance;
static dispatch_once_t onceToken;
dispatch_once (&onceToken, ^{
instance = [super allocWithZone:zone];
});
return instance;
}
// 提供一个sharedMeloPerson方法,供其他类访问
+ (instancetype)sharedMeloPerson
{
return [[self alloc] init];
}
浙公网安备 33010602011771号