iOS ARC 下的单例模式
#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