iOS应用中只会出现一个对象。这种设计模式在系统框架中许多地方都使用了,如NSFileManager、UIApplication等。</p>">单例类是一种特殊的类,在一个进程种只会存在一个该类的对象,在iOS应用中只会出现一个对象。这种设计模式在系统框架中许多地方都使用了,如NSFileManager、UIApplication等。
面试宝典1.0ver/images/singleton.jpg" alt=""></p>">class="alignnone size-full wp-image-495" src="/Upload/Images/2015030516/DAAD96AA8F77F84C.jpg" alt="singleton" width="528" height="174" />
Foundation/Foundation.h&gt;
@interface DVISingleton : NSObject
+ (instancetype)sharedSingleton;
@end
</code></pre>"><span class="hljs-comment">//
<span class="hljs-comment">// DVISingleton.h
<span class="hljs-comment">//
<span class="hljs-comment">// Copyright (c) 2014 长沙戴维营教育. All rights reserved.
<span class="hljs-comment">//
<span class="hljs-preprocessor">#import <span class="hljs-title"><Foundation/Foundation.h>
<span class="hljs-class"><span class="hljs-keyword">@interface <span class="hljs-title">DVISingleton : <span class="hljs-title">NSObject
+ (instancetype)sharedSingleton;
<span class="hljs-keyword">@end
</code></pre>">//
// DVISingleton.h
//
// Copyright (c) 2014 长沙戴维营教育. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface DVISingleton : NSObject
+ (instancetype)sharedSingleton;
@end
实现文件:
线程安全,只允许执行依次
static dispatch_once_t onceToken;
dispatch_once(&amp;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
</code></pre>"><span class="hljs-comment">//
<span class="hljs-comment">// DVISingleton.m
<span class="hljs-comment">//
<span class="hljs-comment">// Copyright (c) 2014 长沙戴维营教育. All rights reserved.
<span class="hljs-comment">//
<span class="hljs-preprocessor">#import <span class="hljs-title">"DVISingleton.h"
<span class="hljs-class"><span class="hljs-keyword">@implementation <span class="hljs-title">DVISingleton
+ (instancetype)sharedSingleton
{
<span class="hljs-keyword">static DVISingleton *sharedObject = <span class="hljs-literal">nil;
<span class="hljs-comment">//线程安全,只允许执行依次
<span class="hljs-keyword">static <span class="hljs-built_in">dispatch_once_t onceToken;
<span class="hljs-built_in">dispatch_once(&onceToken, ^{
<span class="hljs-comment">//使用父类的allocWithZone:方法创建对象
sharedObject = [[<span class="hljs-keyword">super allocWithZone:<span class="hljs-literal">NULL] init];
});
<span class="hljs-keyword">return sharedObject;
}
- (<span class="hljs-keyword">id)init
{
<span class="hljs-keyword">if (<span class="hljs-keyword">self = [<span class="hljs-keyword">super init]) {
}
<span class="hljs-keyword">return <span class="hljs-keyword">self;
}
+ (<span class="hljs-keyword">id)allocWithZone:(<span class="hljs-keyword">struct _NSZone *)zone
{
<span class="hljs-keyword">return [<span class="hljs-keyword">self sharedSingleton];
}
- (<span class="hljs-keyword">id)copy
{
<span class="hljs-keyword">return <span class="hljs-keyword">self;
}
- (<span class="hljs-keyword">void)dealloc
{
}
<span class="hljs-keyword">@end
</code></pre>">//
// 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
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
</code></pre>"><span class="hljs-preprocessor">#import <span class="hljs-title">"DVISingleton.h"
<span class="hljs-class"><span class="hljs-keyword">@implementation <span class="hljs-title">DVISingleton
+ (instancetype)sharedSingleton
{
<span class="hljs-keyword">static DVISingleton *sharedObject = <span class="hljs-literal">nil;
<span class="hljs-comment">//线程安全,只允许执行依次
<span class="hljs-keyword">static <span class="hljs-built_in">dispatch_once_t onceToken;
<span class="hljs-built_in">dispatch_once(&onceToken, ^{
<span class="hljs-comment">//使用父类的allocWithZone:方法创建对象
sharedObject = [[<span class="hljs-keyword">super allocWithZone:<span class="hljs-literal">NULL] init];
});
<span class="hljs-keyword">return sharedObject;
}
+ (<span class="hljs-keyword">id)allocWithZone:(NSZone *)zone {
<span class="hljs-keyword">return [[<span class="hljs-keyword">self sharedSingleton] retain];
}
- (<span class="hljs-keyword">id)copyWithZone:(NSZone *)zone {
<span class="hljs-keyword">return <span class="hljs-keyword">self;
}
- (<span class="hljs-keyword">id)retain {
<span class="hljs-keyword">return <span class="hljs-keyword">self;
}
- (<span class="hljs-keyword">unsigned)retainCount {
<span class="hljs-keyword">return UINT_MAX; <span class="hljs-comment">//denotes an object that cannot be released
}
- (oneway <span class="hljs-keyword">void)release {
<span class="hljs-comment">// never release
}
- (<span class="hljs-keyword">id)autorelease {
<span class="hljs-keyword">return <span class="hljs-keyword">self;
}
- (<span class="hljs-keyword">id)init {
<span class="hljs-keyword">if (<span class="hljs-keyword">self = [<span class="hljs-keyword">super init]) {
}
<span class="hljs-keyword">return <span class="hljs-keyword">self;
}
- (<span class="hljs-keyword">void)dealloc {
[<span class="hljs-keyword">super dealloc];
}
<span class="hljs-keyword">@end
</code></pre>">#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