// 单例
+ (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

ARC模式下的单例写法的更多相关文章

  1. ARC模式下的内存泄露问题

    ARC模式下的内存泄露问题 iOS提供的ARC 功能很大程度上简化了编程,让内存管理变得越来越简单,但是ARC并不是说不会发生内存泄露,使用不当照样会发生. 以下列举两种内存泄露情况: 死循环造成的内 ...

  2. 在arc模式下 CGImage 释放问题

    //大图bigImage //定义myImageRect,截图的区域 if (imagecount >= 3) { CGRect myImageRect; if (i.size.width< ...

  3. 另一鲜为人知的单例写法-ThreadLocal

    另一鲜为人知的单例写法-ThreadLocal 源代码范例 当我阅读FocusFinder和Choreographer的时候,我发现这两类的单例实现和我们寻经常使用双重检查锁非常不一样.而是用来一个T ...

  4. Egret中的三种单例写法

    1 普通的单例写法 as3中也是这么个写法. 缺点:每个单例类里都要写instance和getInstance. class Single{ private static instance:Singl ...

  5. xcode4.3.2 arc模式下导入非arc的文件 转

    在arc模式下,我们经常会用到非arc的类库,此时我们可以在Compile Sources下对该文件进行编辑加入 -fno-objc-arc   如图中所示,就可以使用非arc的类库了   转:htt ...

  6. 1.ARC模式下如何兼容非ARC的类

    ARC模式下如何兼容非ARC的类 :转变为ARC的, -f-objc-arc 非ARC模式下如何兼容ARC的类 :转变为非ARC -fno-objc-arc

  7. 【iOS】ARC-MRC下的单例及其应用

    单例的应用十分普遍,单例模式使一个类仅仅有一个实例. *易于供外界訪问. *方便控制实例个数,节约系统资源. *OC中的常见单例: 如:UIApplication,  NSNotificationCe ...

  8. Unity 单例写法

    借鉴自:http://www.cnblogs.com/CodeCabin/p/unity_global_manager.html 实现复杂一些的全局控制,如切换游戏关卡等操作,更常用的方式是使用单例类 ...

  9. 结合FireBreath在Chrome/FireFox的多进程模式下崩溃一例

    FireBreath是跨浏览器跨操作系统的插件方案,它封装了ActiveX和NPAPI的插件接口,使用统一的API来暴露JSAPI.Chrome和FireFox使用NPAPI,IE使用ActiveX. ...

随机推荐

  1. 上传代码到cocoapod ,自己的框架提供给开发者使用

    1.注册trunk 1 $sudo gem install cocoapods 1 pod trunk register 382782411@qq.com 'Henry519'  --verbose ...

  2. c#对象初始化

    class test:IEquatable<test> { public int aa { get; set; } public string bb { get; set; } publi ...

  3. GitHub Windows客户端部署

    下载网址:https://windows.github.com 点击下载按钮即可下载一个小程序,这个小程序会去服务器端下载完整的Windows版客户端,然后自动安装. 如果安装出错的话,那就打开IE浏 ...

  4. 处理safari缓存的办法

    window.onpageshow = function(event) {        if (event.persisted) {             alert("From bac ...

  5. 新浪微博登录接口(PHP版)

    CI框架下 新浪微博登录接口完整版说明:本贴只适合CI框架.功能实现:登录接口跳转链接成功,获取用户信息(包括最重要的u_id)成功,将用户与本地平台连接起来,用户登录成功后信息的存储,本地数据库第三 ...

  6. [python]类与类中的列表

    最近在用类中的列表时出现一件怪事 实例2中的列表,竟然有实例1中的数据. 查了半天发现是list的append方法的问题. 将全部的list.append(value) 换成 list = list ...

  7. Winform与WPF对话框(MessageBox, Dialog)之比较

    Winform:使用System.Windows.Forms命名空间中相应控件; WPF则调用Microsoft.Win32. MessageBox: // WinForm private void ...

  8. c# 如何通过反射 获取\设置属性值、

    //定义类public class MyClass{public int Property1 { get; set; }}static void Main(){MyClass tmp_Class = ...

  9. POJ 2886 Who Gets the Most Candies? 线段树

    题目: http://poj.org/problem?id=2886 左右转的果断晕,题目不难,关键是准确的转啊转.因为题目要求输出约数个数最多的数,所以预处理[1,500000]的约数的个数就行了. ...

  10. iOS开发之国际化

    iOS 国际化.根据系统不同的语言自动切换. 首先.选择项目 Add new file -->iOS -->Resource -->Strings File  . 命名为Locali ...