iOS 单例模式简单实例
单例模式主要实现唯一实例,存活于整个程序范围内,一般存储用户信息经常用到单例,比如用户密码,密码在登录界面用一次,在修改密码界面用一次,而使用单例,就能保证密码唯一实例。如果不用单例模式,init 两个的实例的堆栈地址不一样,所以存放的数据的位置也不一样,当其中一个数据改变,另一个数据依然不变。单例模式的代码如下
.h文件
#ifndef Singleton_h
#define Singleton_h @interface Singleton : NSObject
@property (nonatomic, copy) NSString *pass;
+ (Singleton *) sharedInstance; @end
.m文件
#import <Foundation/Foundation.h>
#import "Singleton.h" @implementation Singleton
static id sharedSingleton = nil;
+ (id)allocWithZone:(struct _NSZone *)zone
{
if (sharedSingleton == nil) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedSingleton = [super allocWithZone:zone];
});
}
return sharedSingleton;
} - (id)init
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedSingleton = [super init];
});
return sharedSingleton;
} + (instancetype)sharedInstance
{
return [[self alloc] init];
}
+ (id)copyWithZone:(struct _NSZone *)zone
{
return sharedSingleton;
}
+ (id)mutableCopyWithZone:(struct _NSZone *)zone
{
return sharedSingleton;
} @end
宏实现单例
#ifndef Singleton_m_h
#define Singleton_m_h // 帮助实现单例设计模式 // .h文件的实现
#define SingletonH(methodName) + (instancetype)shared##methodName; // .m文件的实现
#if __has_feature(objc_arc) // 是ARC
#define SingletonM(methodName) \
static id _instace = nil; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
if (_instace == nil) { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
} \
return _instace; \
} \
\
- (id)init \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super init]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##methodName \
{ \
return [[self alloc] init]; \
} \
+ (id)copyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
} \
\
+ (id)mutableCopyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
} #else // 不是ARC #define SingletonM(methodName) \
static id _instace = nil; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
if (_instace == nil) { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
} \
return _instace; \
} \
\
- (id)init \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super init]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##methodName \
{ \
return [[self alloc] init]; \
} \
\
- (oneway void)release \
{ \
\
} \
\
- (id)retain \
{ \
return self; \
} \
\
- (NSUInteger)retainCount \
{ \
return ; \
} \
+ (id)copyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
} \
\
+ (id)mutableCopyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
}
#endif /* Singleton_m_h */
iOS 单例模式简单实例的更多相关文章
- Hibernate(二)__简单实例入门
首先我们进一步理解什么是对象关系映射模型? 它将对数据库中数据的处理转化为对对象的处理.如下图所示: 入门简单实例: hiberante 可以用在 j2se 项目,也可以用在 j2ee (web项目中 ...
- iOS单例模式(Singleton)写法简析
单例模式的意思就是只有一个实例.单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类. 1.单例模式的要点: 显然单例模式的要点有三个:一是某个类只能有一个实例: ...
- IOS单例模式(Singleton)
IOS单例模式(Singleton) 单例模式的意思就是只有一个实例.单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类. 1.单例模式的要点: 显然单例模 ...
- appium+robotframework的简单实例
在上篇文章中,我们搭建好了appium+robotframework的环境,这篇文章中主要是一个简单实例. 一.测试用例编写前提 1.模拟器(或手机)连接电脑 adb devices ...
- iOS CAReplicatorLayer 简单动画
代码地址如下:http://www.demodashi.com/demo/11601.html 写在最前面,最近在看学习的时候,偶然间发现一个没有用过的Layer,于是抽空研究了下,本来应该能提前记录 ...
- 最新 Eclipse IDE下的Spring框架配置及简单实例
前段时间开始着手学习Spring框架,又是买书又是看视频找教程的,可是鲜有介绍如何配置Spring+Eclipse的方法,现在将我的成功经验分享给大家. 本文的一些源代码来源于码农教程:http:// ...
- 修改js confirm alert 提示框文字的简单实例
修改js confirm alert 提示框文字的简单实例: <!DOCTYPE html> <html> <head lang="en"> & ...
- 利用navicat创建存储过程、触发器和使用游标的简单实例
利用navicat创建存储过程.触发器和使用游标的简单实例 标签: navicat存储过程触发器mysql游标 2013-08-03 21:34 15516人阅读 评论(1) 收藏 举报 分类: 数 ...
- 【转】Android Https服务器端和客户端简单实例
转载地址:http://blog.csdn.net/gf771115/article/details/7827233 AndroidHttps服务器端和客户端简单实例 工具介绍 Eclipse3.7 ...
随机推荐
- ArcGIS10.3+Oracle12C+ArcGIS Server10.3安装布署(之一)
1.安装Oracle12C 2.配置Oracle12C的PDB监听(1)原始listener.ora文件 改为: (2)原始tnsnames.ora文件 改为: 3.连接Oracle12C的PDB数据 ...
- git pull 错误:The following untracked working tree files would be overwritten by merge
错误描述: $ git pull origin alphaFrom https://github.com/shirley-wu/HeartTrace * branch alpha ...
- javascript promise编程
在loop中使用promise: https://stackoverflow.com/questions/17217736/while-loop-with-promises
- 分享:将WDCP中的PHP5.2 1.7升级到PHP 5.3的方法
将wdcp中php 5.2.17升级到php 5.3方法: 1.首先下载wdcp php5.3升级脚本 wget http://down.wdlinux.cn/in/php_up53.sh 2.进行安 ...
- windows多线程同步
概述 任何单个应用程序都不能完全使该处理器达到满负荷.当一个线程遇到较长等待时间事件时,同步多线程还允许另一线程中的指令使用所有执行单元.例如,当一个线程发生高速缓存不命中,另一个线程可以继续执行.同 ...
- 转:oracle 事务
原文地址:http://blog.csdn.net/junmail/article/details/5556561 关于Oracle事务的总结 1.什么是事务,事务的特性是什么? 事务的任务便是使数据 ...
- 使用 JSONModel
Magical Data Modelling Framework for JSON https://github.com/icanzilb/JSONModel New: In version 0.12 ...
- Linux ip命令详解
ip命令式用来配置网卡ip信息的命令,且是未来的趋势,重启网卡后IP失效 ip常见命令参数 Usage: ip [ OPTIONS ] OBJECT { COMMAND | help } ip [ - ...
- K8S Deployment 命令
创建 Deployment kubectl create -f https://kubernetes.io/docs/user-guide/nginx-deployment.yaml --record ...
- Ubuntu下安装指定版本的mysql
1.编辑/etc/apt/sources.list和/etc/apt/sources.list.save, 手动加上deb http://archive.ubuntu.com/ubuntu trust ...