iOS开发——实用技术OC篇&单例模式的实实现(ACR&MRC)
单例模式的实实现(ACR&MRC)
在iOS开发中单例模式是一种非常常见的模式,虽然我们自己实现的比较少,但是,系统却提供了不少的到来模式给我们用,比如最常见的UIApplication,Notification等,
那么这篇文章就简单介绍一下,我们开发中如果想要实现单例模式要怎么去实现!
单例模式顾名思义就是只有一个实例,它确保一个类只有一个实例,并且自行实例化并向整个系统提供这个实例。它经常用来做应用程序级别的共享资源控制。这个模式使用频率非常高,通过一个单例类,可以实现在不同窗口之间传递数据。
在objective-c(MRC)中要实现一个单例类,至少需要做以下四个步骤:
- 1、为单例对象实现一个静态实例,并初始化,然后设置成nil,
- 2、实现一个实例构造方法检查上面声明的静态实例是否为nil,如果是则新建并返回一个本类的实例,
- 3、重写allocWithZone方法,用来保证其他人直接使用alloc和init试图获得一个新实例的时候不产生一个新实例,
- 4、适当实现allocWitheZone,copyWithZone,release和autorelease
例子:为RootViewController创建一个单例函数:
代码如下,可直接拷贝到头文件中
#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\
{\
;\
}
但是在非ARC中也就是MRC中实现的方式却不一样,我们需要做对应的内存管理。
MRC要重写四个方法:
-(oneway void)release
{
}
-(instancetype)autorelease
{
return self;
}
-(instancetype)retain{
return self;
}
-(NSUInteger)retainCount{
;
}
iOS开发——实用技术OC篇&单例模式的实实现(ACR&MRC)的更多相关文章
- iOS开发——实用技术OC篇&简单抽屉效果的实现
简单抽屉效果的实现 就目前大部分App来说基本上都有关于抽屉效果的实现,比如QQ/微信等.所以,今天我们就来简单的实现一下.当然如果你想你的效果更好或者是封装成一个到哪里都能用的工具类,那就还需要下一 ...
- ios开发——实用技术OC篇&地图与定位
地图与定位 11.1 iOS定位服务 11.2 iOS地图 11.3 Web地图 1 iOS定位服务 iOS中有三个定位服务组件: Wifi定位,通过查询一个Wifi路由器的地理位置的信息.比较省电, ...
- iOS开发——实用技术OC篇&8行代码教你搞定导航控制器全屏滑动返回效果
8行代码教你搞定导航控制器全屏滑动返回效果 前言 如果自定了导航控制器的自控制器的leftBarButtonItem,可能会引发边缘滑动pop效果的失灵,是由于 self.interactivePop ...
- iOS开发——实用技术OC篇&事件处理详解
事件处理详解 一:事件处理 事件处理常见属性: 事件类型 @property(nonatomic,readonly) UIEventType type; @property(nonatomic ...
- ios开发——实用技术OC篇》倒计时实现的两种方法
倒计时实现的两种方法 timeFireMethod函数,timeFireMethod进行倒计时的一些操作,完成时把timer给invalidate掉就ok了,代码如下: secondsCountDow ...
- iOS开发——实战OC篇&环境搭建之Xib(玩转UINavigationController与UITabBarController)
iOS开发——实战OC篇&环境搭建之Xib(玩转UINavigationController与UITabBarController) 前面我们介绍了StoryBoard这个新技术,和纯技术 ...
- iOS开发——实战OC篇&环境搭建之纯代码(玩转UINavigationController与UITabBarController)
iOS开发——实战OC篇&环境搭建之纯代码(玩转UINavigationController与UITabBarController) 这里我们就直接上实例: 一:新建一个项目singleV ...
- ios开发——实用技术OC-Swift篇&触摸与手势识别
iOS开发学习之触摸事件和手势识别 iOS的输入事件 触摸事件 手势识别 手机摇晃 一.iOS的输入事件 触摸事件(滑动.点击) 运动事件(摇一摇.手机倾斜.行走),不需要人为参与的 远程控制 ...
- iOS开发——实战OC篇&环境搭建之StoryBoard(玩转UINavigationController与UITabBarController)
环境搭建之StoryBoard(玩转UINavigationController与UITabBarController) 研究了这么就IOS开发,都没有所处一个像样或者自己忙一点的项目.最近自 ...
随机推荐
- python中的多线程【转】
转载自: http://c4fun.cn/blog/2014/05/06/python-threading/ python中关于多线程的操作可以使用thread和threading模块来实现,其中th ...
- Cassandra + Eclipse + Hadoop
配置好eclipse连接hadoop: 参考:http://www.powerxing.com/hadoop-build-project-using-eclipse/ http://blog.csdn ...
- 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程08:虚拟键盘实现》--本系列完结
8.虚拟键盘实现 概述: 硬键盘就是物理键盘,平时敲的那种.软键盘是虚拟的键盘,不是在键盘上,而是在"屏幕"上.虚拟按键就是虚拟键盘的一部分,根据功能需求,提供部分按键效果的UI可 ...
- 《Linux设备驱动程序》 笔记2
驱动代码hello.c #include <linux/init.h> #include <linux/module.h> static int hello_init(void ...
- 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误
原因1:给定目录下jvm.dll不存在. 对策:(1)重新安装jre或者jdk并配置好环境变量.(2)copy一个jvm.dll放在该目录下. 原因2:eclipse的版本与jre或者jdk版本不一致 ...
- 轻松学习Linux之自动执行任务
在 Linux 中,任务可以被配置在指定的时间段.指定的日期.或系统平均载量低于指定的数量时自动运行,系统管理员可使用自动化的任务来执行定期备份.监控系统.运行定制脚本等,往往初学者都是win用户,习 ...
- 【LoadRunner】安装LoadRunner时提示缺少vc2005_sp1_with_atl_fix_redist解决方案
我的电脑在安装UFT时,被要求需要卸载本机上安装的LoadRunner11,当LoadRunner11被卸载后,进行重新安装LoadRunner11时,会报缺少vc2005_sp1_with_atl_ ...
- <转载>gcc/g++编译
转载于:http://www.cnblogs.com/yc_sunniwell/archive/2010/07/22/1782678.html 1. gcc/g++在执行编译工作的时候,总共需要4步 ...
- Unity3D Script Keynote
[Unity3D Script Keynote] 1.创建GameObject if(GUILayout.Button("创建立方体",GUILayout.Height(50))) ...
- hdu 1260 Tickets
http://acm.hdu.edu.cn/showproblem.php?pid=1260 题目大意:n个人买票,每个人买票都花费时间,相邻的两个人可以一起买票以节约时间: 所以一个人可以自己买票也 ...