1。 arc环境下:
提供全局变量
static Tool *instance
+(instance)allocWithZone:(struct _NSZone *)zone
{
if(instance==nil){
instance=[super allocWithZone:zone];
}
return instance;
}
加互斥锁的方法
+(instance)allocWithZone:(struct _NSZone *)zone
{
@synchronized(self){
instance=[super allocWithZone:zone];
}
return instance;
}
+(instance)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance=[super allocWithZone:zone];
});
return instance;
}
创建类方法
+(instance)shareTool
{
return【self alloc】init;}
{
return instance;
}
-(id)mutableCopyWithZone:(NSZone *)zone
{
return instance;
}
2.MRC环境下
先修改环境为MRC
static Tool *instance
+(instance)allocWithZone:(struct _NSZone *)zone
{
if(instance==nil){
instance=[super allocWithZone:zone];
}
return instance;
}
加互斥锁的方法
+(instance)allocWithZone:(struct _NSZone *)zone
{
@synchronized(self){
instance=[super allocWithZone:zone];
}
return instance;
}
+(instance)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance=[super allocWithZone:zone];
});
return instance;
}
创建类方法
+(instance)shareTool
{
return【self alloc】init;}
{
return instance;
}
-(id)mutableCopyWithZone:(NSZone *)zone
{
return instance;
}