天天看點

使用GCD的dispatch_once建立單例

之前一篇《Objective-C的單例模式(singleton)》

http://arthurchen.blog.51cto.com/2483760/642536

介紹了建立單例的方式,不過後來發現其實在ios 4.0後有更加簡單的方式。

就是使用GCD的功能

代碼如下:

 + (instantClass *)sharedClient {

    static instantClass *_sharedClient = nil;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        _sharedClient = [[instantClass alloc] init];

    });

本文轉自 arthurchen 51CTO部落格,原文連結:http://blog.51cto.com/arthurchen/1059752,如需轉載請自行聯系原作者

繼續閱讀