首页 > 移动平台 > 详细

iOS ARC 下的单例模式

时间:2015-08-08 19:42:05      阅读:182      评论:0      收藏:0      [点我收藏+]

#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

 

iOS ARC 下的单例模式

原文:http://www.cnblogs.com/tangranyang/p/4713678.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!