码迷,mamicode.com
首页 > 移动开发 > 详细

iOS ARC 下的单例模式

时间:2015-08-08 19:43:40      阅读:121      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!