ARC模式下的单例写法
2014-03-18 10:41
381 查看
// 单例 + (id)sharedInstance { __strong static id sharedObject = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedObject = [[self alloc] init]; }); return sharedObject; }
dispatch_once
Executes a block object once and only once for the lifetime of an application.
void dispatch_once(
dispatch_once_t *predicate,
dispatch_block_t block);
Parameters
predicate
A pointer to a dispatch_once_t structure that is used to test whether the block has completed or not.
block
The block object to execute once.
Discussion
This function is useful for initialization of global data (singletons) in an application. Always call this function before using or testing any variables that are initialized by the block.
If called simultaneously from multiple threads, this function waits synchronously until the block has completed.
The predicate must point to a variable stored in global or static scope. The result of using a predicate with automatic or dynamic storage (including Objective-C instance variables) is undefined.
Availability
Available in iOS 4.0 and later.
Declared In
dispatch/once.h
相关文章推荐
- hdu 1575 矩阵快速幂
- Lync Server 2013移动设备架构
- sed命令使用
- tomcat的类加载机制
- 网站的下次自动登录功能的实现方法
- IP地址:A类,B类,C类
- MatLab2012b/MatLab2013b分类器大全(svm,knn,随机森林等)
- 项目一三角形类2
- 处理大整数------计算n!(阶层)
- HTTP 404 - 未找到文件 怎么样解决
- SQL中group by的用法
- Lync Server 2013群聊架构
- NSLog的格式
- Linux系统资源限制
- 位运算符巧妙用法。
- Android四大基本组件介绍与生命周期
- Myeclipse中JSP页面快捷键注释失效解决方法
- tomcat 安装时出现 Failed to install Tomcat7 service
- hdu 2553 n皇后问题
- JDBC常见面试题集锦(一)