代码如下,可直接拷贝到头文件中

#define singleton_h(name) +(instancetype)shared##name
# if __has_feature(objc_arc) //ARC #define singleton_m(name) \
static id _instance;\
+(id)allocWithZone:(struct _NSZone *)zone\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
return _instance;\
}\
\
+(instancetype)shared##name\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [[self alloc] init];\
});\
return _instance;\
}\
\
+(id)copyWithZone:(struct _NSZone *)zone\
{\
return _instance;\
}
#else //非ARC
#define singleton_m(name) \
static id _instance;\
+(id)allocWithZone:(struct _NSZone *)zone\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
return _instance;\
}\
\
+(instancetype)shared##name\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [[self alloc] init];\
});\
return _instance;\
}\
\
+(id)copyWithZone:(struct _NSZone *)zone\
{\
return _instance;\
}\
-(oneway void)release\
{\
\
}\
-(instancetype)autorelease\
{\
return _instance;\
}\
-(instancetype)retain\
{\
return _instance;\
}\
-(NSUInteger)retainCount\
{\
return ;\
} #endif

MRC要重写四个方法:

-(oneway void)release

{

}

-(instancetype)autorelease

{

return self;

}

-(instancetype)retain{

return self;

}

-(NSUInteger)retainCount{

return 1;

}

ARC和MRC实现单例模式的更多相关文章

  1. 单例模式 - GCD 、兼容ARC和MRC

    单例模式 - GCD .兼容ARC和MRC 单例模式的作用: 1,能够保证在程序执行过程.一个类仅仅有一个实例,并且该实例易于供外界訪问 2,从而方便地控制了实例个数,并节约系统资源 单例模式的使用场 ...

  2. iOS开发ARC与MRC下单例的完整写法与通用宏定义

    #import "XMGTool.h" /** * 1:ARC下的完整的单例写法:alloc内部会调用+(instancetype)allocWithZone:(struct _N ...

  3. ARC以及MRC中setter方法

    ARC以及MRC中setter方法的差异 有时候,你会需要重写setter或者getter方法,你知道么,ARC与MRC的setter方法是有着差异的呢. 先看下MRC下的setter方法: 在看下A ...

  4. ARC 和 MRC 小结

    ARC 和 MRC 内存管理 从 MRC—>ARC 就是将内存管理部分,从开发者的函数中转移到函数外部的runtime 中.由于 runtime 的开发简单,逻辑层次高,所以 runtime 的 ...

  5. ARC、MRC混编

    Xcode5之后,新建iOS工程,默认都是ARC模式,但是有时候我们的项目中需要用到一些第三方框架,我们下载下来却发现是非ARC的,这时候我们需要进行ARC和MRC混编. 第一种方式: Edit-&g ...

  6. iOS内存管理 ARC与MRC

    想驾驭一门语言,首先要掌握它的内存管理特性.iOS开发经历了MRC到ARC的过程,下面就记录一下本人对iOS内存管理方面的一些理解. 说到iOS开发,肯定离不开objective-c语言(以下简称OC ...

  7. DES加密(支持ARC与MRC)

    DES加密(支持ARC与MRC) 源文件: YXCrypto.h 与 YXCrypto.m // // YXCrypto.h // 用秘钥给字符串加密或者解密 // // Created by You ...

  8. ARC以及MRC中setter方法的差异

    ARC以及MRC中setter方法的差异 有时候,你会需要重写setter或者getter方法,你知道么,ARC与MRC的setter方法是有着差异的呢. 先看下MRC下的setter方法: 在看下A ...

  9. MRC下单例模式的内存问题与ARC实现

    单例模式保证一个类只能拥有一个静态的实例,类负责创建与维护这个实例,并提供一个统一的静态(类方法)访问方式,并封锁了这个类外部的代码对这个类对象的创建. .h文件: #import <Found ...

随机推荐

  1. 【HTML】Advanced1:Text: Time, Mark, and "Presentational"

    1.Exploring the depths of HTML5 2.</time> <p>Written by Doctor Who on <time datetime= ...

  2. linux驱动程序之电源管理之新版linux系统设备架构中关于电源管理方式的变更

    新版linux系统设备架构中关于电源管理方式的变更 based on linux-2.6.32 一.设备模型各数据结构中电源管理的部分 linux的设备模型通过诸多结构体来联合描述,如struct d ...

  3. 如何使用 RDP 或 SSH 连接到 Azure 虚拟机

    使用 RDP 或 SSH 连接到 Azure 虚拟机 本文简要概述了如何使用远程桌面控制协议 (RDP) 或安全外壳(Secure Shell,SSH)客户端登录 Azure 虚拟机.它还包括要求和故 ...

  4. Dumpbin的使用方法

    推荐:http://blog.csdn.net/blpluto/article/details/5706757

  5. Storm系列(三)Topology提交过程

    提交示例代码: 1  ); // 设置一个ack线程 9      conf.setDebug(true); // 设置打印所有发送的消息及系统消息 10      StormSubmitter.su ...

  6. Spring-demo1(初学者的尝试,2015.03.19)

    项目结构: 源代码如下: package com.bean; public interface Person { public void Speak(); } package com.bean; pu ...

  7. HW2.22

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  8. C++中字符串的结尾标志\0

    \0是C++中字符串的结尾标志,存储在字符串的结尾,它虽然不计入串长,但要占一个字节的内存空间.在百度百科中查看\0词条,会有这样一句话:c/c++中规定字符串的结尾标志为'\0'.有人可能认为,在C ...

  9. 配置 Web Deploy 的步骤 -摘自网络

    今天的文章里,我会介绍Microsoft Web Deploy—一个采用全面的发布和部署机制的免费服务器技术.Web Deploy不仅仅让你发布文件—还可以部署数据库结构/数据,运行变更的数据库脚本, ...

  10. 每个android项目都应该使用的android 库

    http://blog.teamtreehouse.com/android-libraries-use-every-project A good developer knows to never re ...