Objective-C 单例模式
2015-06-27 20:09
459 查看
加锁的写法:
第一次实例化创建Lock free:
GCD写法:
+ (id)allocWithZone:(struct _NSZone *)zone
{
return [self sharedManager];
}
+ (id)copyWithZone:(struct _NSZone *)zone
{
return self;
}
static id sharedMyManager; + (id)sharedThemeManager { @synchronized(self) { if(sharedMyManager == nil) { sharedMyManager = [[self alloc] init]; } } return sharedMyManager; }
第一次实例化创建Lock free:
static id sharedMyManager; + (void)initialize { static BOOL initialized = NO; if (initialized == NO) { initialized = YES; sharedMyManager = [[self alloc] init]; } }
GCD写法:
static id sharedMyManager; + (id)sharedManager { static dispatch_once_t once; dispatch_once(&once, ^{sharedMyManager = [[self alloc] init];}); return sharedMyManager; }
+ (id)allocWithZone:(struct _NSZone *)zone
{
return [self sharedManager];
}
+ (id)copyWithZone:(struct _NSZone *)zone
{
return self;
}
相关文章推荐
- [学习笔记—Objective-C]《Objective-C 程序设计 第6版》第四章 数据类型和表达式
- objective-c连接MySQL数据库
- Objective-C(五、@class,id类型,类对象构造方法,加载和初始化,description,Logging宏)——iOS开发基础
- NSArray、NSMutableArray基本用法
- Objective-C 快速入门--基础(二)
- Objective-C 快速入门--基础(二)
- Gson is a Java library that can be used to convert Java Objects into their JSON representation.
- 黑马程序员——Objective-C语言知识整理——类方法与对象方法
- 黑马程序员——Objective-C语言知识整理——封装、继承与多态
- js window.open打开新窗口原页面出现[object window]解决办法
- 黑马程序员——Objective-C语言知识整理——类与对象基本知识
- Objective-C总Runtime的那点事儿(一)消息机制
- Objective-C 消息转发 与NSObject中forwardInvocation消息重定向
- OGNL:Object Graph Navigation Language(对象图导航语言)
- Object.toString
- Big Data Counting: How to count a billion distinct objects using only 1.5KB of Memory
- 【iOS开发之Objective-C】面向对象编程--间接
- Objective-C 快速入门--基础(一)
- Objective-C 快速入门--基础(一)
- ORA-31685: Object type DB_LINK:"NCAOODS"."LINK_SNAPSHOTM" failed due to insufficient privileges. Fai