单例类是一种特殊的类。在一个进程种仅仅会存在一个该类的对象,在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辛格尔顿的更多相关文章

  1. Automake

    Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...

  2. Objective C中的ARC的修饰符的使用---- 学习笔记九

    #import <Foundation/Foundation.h> @interface Test : NSObject /** * 默认的就是__strong,这里只是做示范,实际使用时 ...

  3. Objective的字符串拼接 似乎没有Swift方便,但也可以制做一些较为方便的写法

    NSString *str1 = @"字符串1"; NSString *str2 = @"字符串2"; //在同样条件下,Objective的字符串拼接 往往只 ...

  4. [转] 从 C 到 Objective C 入门1

    转自: http://blog.liuhongwei.cn/iphone/objective-c/ 进军iPhone开发,最大的难点之一就是怪异的Objective C语法了.不过,了解之后才发现,原 ...

  5. Objective C运行时(runtime)

    #import <objc/runtime.h> void setBeingRemoved(id __self, SEL _cmd) { NSLog(@"------------ ...

  6. Objective C ARC 使用及原理

    手把手教你ARC ,里面介绍了ARC的一些特性, 还有将非ARC工程转换成ARC工程的方法 ARC 苹果官方文档 下面用我自己的话介绍一下ARC,并将看文档过程中的疑问和答案写下来.下面有些是翻译,但 ...

  7. Objective -C学习笔记之字典

    //字典:(关键字 值) // NSArray *array = [NSArray array];//空数组 // NSDictionary *dictionary = [NSDictionary d ...

  8. 刨根问底Objective-C Runtime

    http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective%5Bnil%5Dc-runtime-(2)%5Bnil%5D-object-and- ...

  9. Objective-C( Foundation框架 一 字符串)

    Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...

  10. Objective C类方法load和initialize的区别

    Objective C类方法load和initialize的区别   过去两个星期里,为了完成一个工作,接触到了NSObject中非常特别的两个类方法(Class Method).它们的特别之处,在于 ...

随机推荐

  1. hdu1042(大数模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042 在网上找了个大数模板方便以后用得到. #include<iostream> #inc ...

  2. RabbitMQ消息队列应用

    RabbitMQ消息队列应用 消息通信组件Net分布式系统的核心中间件之一,应用与系统高并发,各个组件之间解耦的依赖的场景.本框架采用消息队列中间件主要应用于两方面:一是解决部分高并发的业务处理:二是 ...

  3. WPF自定义ListBox样式

    <!--竖向--> <Style x:Key="ListBoxStyle1" TargetType="{x:Type ListBox}"> ...

  4. 怎样将android studio项目导入eclipse

    如今,越来越多的开源项目都是用android studio来开发的,所以源码都与eclipse有所不同. 以下是将android studio项目导入eclipse的一般步骤: 1. 先解压项目: 2 ...

  5. 编程算法 - 字典分词 代码(C)

    字典分词 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 给定字典, 给定一句话, 进行分词. 使用深度遍历(DFS)的方法. 使用一个參数string ...

  6. 使用SVNkit删除版本库的文件

    源网址:http://wiki.svnkit.com/Committing_To_A_Repository Editing Operation: commiting to a repository T ...

  7. zend studio代码字体修改字体和大小.

    第一步:进入设置窗口    windows -> preferences 第二步:进入修改字体的选项卡.    General -> Appearance -> Colors and ...

  8. linux环境下的线程的创建问题

    pthread_create函数用于创建一个线程 函数原型 #include<pthread.h> int pthread_create(pthread_t *restrict tidp, ...

  9. android LinearLayout等view如何获取button效果

    我们可以给LinearLayout以及一切继承自View的控件,设置View.onClickListener监听,例如LInearLayout. 但是我们发现LinearLayout可以执行监听方法体 ...

  10. Java使用LdAP获取AD域用户

    随着我们的习大大上台后,国家在网络信息安全方面就有了非常明显的改变!所以如今好多做网络信息安全产品的公司和须要网络信息安全的公司都会提到用AD域server来验证,这里就简单的研究了一下! 先简单的讲 ...