#import <Foundation/Foundation.h>
@interface RYSingleExample : NSObject<NSCopying>
+(instancetype)singleExample;
@end
#import "RYSingleExample.h"
static id _single;
@implementation RYSingleExample
+(instancetype)singleExample
{
if(_single==nil)
{
@synchronized(self)
{
if (_single==nil) {
_single=[[self alloc]init];
}
}
}
return _single;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
if(_single==nil)
{
@synchronized(self)
{
if (_single==nil) {
_single=[super allocWithZone:zone];
}
}
}
return _single;
}
-(id)copyWithZone:(NSZone *)zone
{
return _single;
}
@end
时间: 2024-10-03 15:01:45