1.声明单例对象
在.h文件
+(SingleCase *)sharedSingleCase;
2.实现单例
在.m文件
SingleCase * singleCase = nil;
+(SingleCase *)sharedSingleCase{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// 第一次
// singleCase = [[SingleCase alloc]init];
// singleCase = [SingleCase allocWithZone:NULL]init];
// singleCase = [[self alloc]init];
// singleCase = [self allocWithZone:NULL]init];
singleCase = [[super allocWithZone:NULL]init];
});
return singleCase;
// 第一次
// singleCase = [[SingleCase alloc]init];
// singleCase = [SingleCase allocWithZone:NULL]init];
// singleCase = [[self alloc]init];
// singleCase = [self allocWithZone:NULL]init];
singleCase = [[super allocWithZone:NULL]init];
});
return singleCase;
}
//=================遵守 <NSCopying,NSMutableCopying>
-(id)copyWithZone:(NSZone *)zone{
return self;
}
-(id)mutableCopyWithZone:(NSZone *)zone{
return self;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
return [SingleCase sharedSingleCase];
}
//防止copy
+ (id)copyWithZone:(NSZone *)zone{
return [SingleCase sharedSingleCase];
}
//防止mutable
+(id)mutableCopyWithZone:(struct _NSZone *)zone
{
return [SingleCase sharedSingleCase];
}
-(oneway void)release
{
}
-(instancetype)autorelease{
return self;
}
-(instancetype)retain{
return self;
}
-(NSUInteger)retainCount{
return NSIntegerMax;
return self;
}
-(id)mutableCopyWithZone:(NSZone *)zone{
return self;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
return [SingleCase sharedSingleCase];
}
//防止copy
+ (id)copyWithZone:(NSZone *)zone{
return [SingleCase sharedSingleCase];
}
//防止mutable
+(id)mutableCopyWithZone:(struct _NSZone *)zone
{
return [SingleCase sharedSingleCase];
}
-(oneway void)release
{
}
-(instancetype)autorelease{
return self;
}
-(instancetype)retain{
return self;
}
-(NSUInteger)retainCount{
return NSIntegerMax;
}