美文网首页
iOS 单例的完整写法

iOS 单例的完整写法

作者: _MoveForward | 来源:发表于2017-08-21 11:02 被阅读81次

import <Foundation/Foundation.h>

@interface MFShareTool : NSObject<NSCopying,NSMutableCopying>

+(instancetype)shareTool;

@end

import "MFShareTool.h"

@implementation MFShareTool

static MFShareTool * _shareTool;
+(instancetype)allocWithZone:(struct _NSZone *)zone{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_shareTool = [super allocWithZone:zone];
});

return _shareTool;

}

+(instancetype)shareTool{
return [[self alloc]init];
}

-(id)copyWithZone:(NSZone *)zone{
return _shareTool;
}
-(id)mutableCopyWithZone:(NSZone *)zone{
return _shareTool;
}
// 判断环境是否为ARC

if __has_feature(objc_arc)

else

-(oneway void)release {

}
-(instancetype)retain{
return _shareTool;
}
-(NSUInteger)retainCount{
return MAXFLOAT;
}

endif

@end
!!! 注意点
单例不能使用继承

相关文章

  • iOS 单例模式

    关于单例模式的详解,看完这几篇,就完全了然了。iOS 单例模式iOS中的单例模式iOS单例的写法

  • iOS 单例的完整写法

    import @interface MFShareTool :...

  • 单例的2种写法

    单例模式是iOS开发中最常用的设计模式,iOS的单例模式有两种官方写法,如下: 1,常用写法 import "Se...

  • 单例的完整写法

    有朋友还在单例的问题上纠结,如何写才算完备,今天在这里写一写。 注意:单例一但创建,整个App的使用过程中都不会被...

  • iOS-两种单例模式的实现

    单例模式是开发中最常用的写法之一,创建一个单例很多办法,iOS的单例模式有两种官方写法,如下: 不使用GCD 某些...

  • ios~单例模式:

    在iOS OC中,一般我们都是用官方推荐的写法来写单例:GCD方式单例 分析单例 static SharedPer...

  • 完整单例模式写法

    前言 关于单例模式,在开发中我们经常使用到,在此作一个记录。既然是单例,那么我们就应该保证通过各种方式初始化创建的...

  • ios 单例写法

  • iOS单例的写法

    参考https://www.jianshu.com/p/6b012ebc10fe .h文件 ```objectiv...

  • IOS单例的写法

    http://blog.sina.com.cn/s/blog_945590aa0102vxhb.html 可以看到...

网友评论

      本文标题:iOS 单例的完整写法

      本文链接:https://www.haomeiwen.com/subject/fhdcdxtx.html