iOS 创建单例
2016-12-07 18:15
281 查看
#import "SingleInstance.h" @interface SingleInstance ()<NSCopying,NSMutableCopying> @end //定义一个当前单例对象的一个实例,并赋值为nil static SingleInstance *instance = nil; @implementation SingleInstance + (instancetype)sharedInstance { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [[self alloc] init]; }); return instance; } /**************真正的单例需重载所有和创建有关的方法**************/ //alloc会触发,防止通过alloc创建一个不同的实例 + (instancetype)allocWithZone:(struct _NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [super allocWithZone:zone]; }); return instance; } - (id)copyWithZone:(nullable NSZone *)zone { return self; } - (id)mutableCopyWithZone:(nullable NSZone *)zone { return self; } /**************手动内存管理需做的操作**************/ - (instancetype)retain { return self; } - (oneway void)release { } - (instancetype)autorelease { return self; } - (NSUInteger)retainCount { return MAXFLOAT; } - (void)dealloc { } @end
相关文章推荐
- IOS -- UIButton 代码创建
- 在 Flash Builder 中创建 iOS 应用程序
- 【详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!】【二】
- 创建IOS 5 News Stand应用程序之一 – 外观
- iOS创建不带nib文件的工程之一--空白工程
- 创建iOS 5 News Stand应用程序之一 外观
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!
- 【iOS-Cocos2d游戏开发之二十一 】自定义精灵类并为你的精灵设置攻击帧以及动画创建!【二】 推荐
- iOS开发技巧:使用Objective-C创建UUID
- 汇集大型Flash/AIR项目和众多SWF创建 iOS
- 创建具有root权限的ios应用
- IOS 入门开发之创建标题栏UINavigationBar的使用(二)
- 【详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!】【一】
- iphone ios 文件系统操作(创建、删除,修改,获取属性)
- 据传:Google 正在为 Android 创建一个类似于 iOS 的“Game Center”
- 【iOS-cocos2d游戏引擎开发之一】搭建cocos2d游戏引擎环境,创建第一个HelloWorld! 推荐
- 创建具有root权限的ios应用
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!
- 创建用于 iPad 的 iOS 幻灯片应用程序
- iOS创建不带nib文件的工程之二--添加视图和视图控制器