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

 

posted @ 2015-08-08 18:48  汤冉阳  阅读(224)  评论(0编辑  收藏  举报