Objective-C辛格尔顿
单例类是一种特殊的类。在一个进程种仅仅会存在一个该类的对象,在iOS应用中仅仅会出现一个对象。这样的设计模式在系统框架中很多地方都使用了。如NSFileManager、UIApplication等。

- 在ARC的环境下,接口文件为:
//
// DVISingleton.h
//
// Copyright (c) 2014 长沙戴维营教育. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface DVISingleton : NSObject
+ (instancetype)sharedSingleton;
@end
实现文件:
//
// DVISingleton.m
//
// Copyright (c) 2014 长沙戴维营教育. All rights reserved.
//
#import "DVISingleton.h"
@implementation DVISingleton
+ (instancetype)sharedSingleton
{
static DVISingleton *sharedObject = nil;
//线程安全。仅仅同意运行依次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//使用父类的allocWithZone:方法创建对象
sharedObject = [[super allocWithZone:NULL] init];
});
return sharedObject;
}
- (id)init
{
if (self = [super init]) {
}
return self;
}
+ (id)allocWithZone:(struct _NSZone *)zone
{
return [self sharedSingleton];
}
- (id)copy
{
return self;
}
- (void)dealloc
{
}
@end
- 在非ARC环境下的实现文件:
<code class="objectivec hljs" data-origin="" <pre><code="" "dvisingleton.h""="" style="margin: 0px; padding: 0px; border: 0px; font-size: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(110, 107, 94); font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important;">#import "DVISingleton.h" @implementation DVISingleton + (instancetype)sharedSingleton
{
static DVISingleton *sharedObject = nil; //线程安全。仅仅同意运行依次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//使用父类的allocWithZone:方法创建对象
sharedObject = [[super allocWithZone:NULL] init];
}); return sharedObject;
} + (id)allocWithZone:(NSZone *)zone {
return [[self sharedSingleton] retain];
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (unsigned)retainCount {
return UINT_MAX; //denotes an object that cannot be released
}
- (oneway void)release {
// never release
}
- (id)autorelease {
return self;
}
- (id)init {
if (self = [super init]) { }
return self;
}
- (void)dealloc {
[super dealloc];
}
@end
版权声明:本文博主原创文章,博客,未经同意不得转载。
Objective-C辛格尔顿的更多相关文章
- Automake
Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...
- Objective C中的ARC的修饰符的使用---- 学习笔记九
#import <Foundation/Foundation.h> @interface Test : NSObject /** * 默认的就是__strong,这里只是做示范,实际使用时 ...
- Objective的字符串拼接 似乎没有Swift方便,但也可以制做一些较为方便的写法
NSString *str1 = @"字符串1"; NSString *str2 = @"字符串2"; //在同样条件下,Objective的字符串拼接 往往只 ...
- [转] 从 C 到 Objective C 入门1
转自: http://blog.liuhongwei.cn/iphone/objective-c/ 进军iPhone开发,最大的难点之一就是怪异的Objective C语法了.不过,了解之后才发现,原 ...
- Objective C运行时(runtime)
#import <objc/runtime.h> void setBeingRemoved(id __self, SEL _cmd) { NSLog(@"------------ ...
- Objective C ARC 使用及原理
手把手教你ARC ,里面介绍了ARC的一些特性, 还有将非ARC工程转换成ARC工程的方法 ARC 苹果官方文档 下面用我自己的话介绍一下ARC,并将看文档过程中的疑问和答案写下来.下面有些是翻译,但 ...
- Objective -C学习笔记之字典
//字典:(关键字 值) // NSArray *array = [NSArray array];//空数组 // NSDictionary *dictionary = [NSDictionary d ...
- 刨根问底Objective-C Runtime
http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective%5Bnil%5Dc-runtime-(2)%5Bnil%5D-object-and- ...
- Objective-C( Foundation框架 一 字符串)
Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...
- Objective C类方法load和initialize的区别
Objective C类方法load和initialize的区别 过去两个星期里,为了完成一个工作,接触到了NSObject中非常特别的两个类方法(Class Method).它们的特别之处,在于 ...
随机推荐
- hdu2569(递推dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2569 分析: f(n),n个珠子的合格数:a(n),n个珠子,最后2个相同的合格数:b(n),n个珠子 ...
- Linux Server
Linux Server CentOS 6.3下配置iSCSI网络存储 摘要: 一.简介iSCSI(internet SCSI)技术由IBM公司研究开发,是一个供硬件设备使用的.可以在IP协议的上层运 ...
- ERROR: Error in Log_event::read_log_event(): 'read error', data_len: 438, event_type: 2
分析从库1062问题,解析从库binlog日志,报错如下 [root@xxxdb0402 tmp]# mysqlbinlog mysql-bin.004271 > 4.log ERROR: Er ...
- 自己定义 ViewGroup 支持无限循环翻页之三(响应回调事件)
大家假设喜欢我的博客,请关注一下我的微博,请点击这里(http://weibo.com/kifile),谢谢 转载请标明出处,再次感谢 ################################ ...
- 让CentOS系统时间同步
CentOS系统时间同步的步骤如下: 新装的CentOS系统服务器可能设置了错误的,需要调整时区并调整时间. 如下是CentOS系统使用NTP来从一个时间服务器同步cp /usr/share/zone ...
- Oracle ORA-01034,ORA-27101,ORA-00600
本机IP地址:192.168.1.163 [oracle@rtest ~]$ sqlplus /nolog SQL*Plus: Release 10.2.0.2.0 - Production on S ...
- BIEE11g BI_server Jvm參数调整
1.找到user_projects\domains\bifoundation_domain\bin文件夹 2.复制startWeblogic.sh为新的文件startAdminWeblogic.sh, ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block 开发人员经常编写需要安全功能的应用程序.这些应用程序 ...
- BZOJ 2002 HNOI2010 弹飞羊 块
标题效果,LCT解释版本:见 http://blog.csdn.net/popoqqq/article/details/38849471 如今,用一只手滑动块,并再次改写这个问题0.0 块短啊 将进入 ...
- JAVA先进-设置(1)
>Arrays 基本阵列 1.常见的数组产生于main() 函数,数组下标的索引不能超过0到int的范围 2.当程序试图訪问数组的第一个或者最后一个数据的时候,会发生ArrayIndexOutO ...