iOS单利创建的方法

我们在使用单例的时候有两种方法@synchronized,GCD,往往人们使用@synchronized,但是推荐使用GCD:

第一种(@synchronized):

+ (id)sharedInstance
{
    static Instance *obj = nil;
    @synchronized([Instance class])
    {
        if(!obj)
            obj = [[Instance alloc] init];
    }
    return obj;
}

第二种(GCD--推荐):

+ (id)sharedInstance
{
    static dispatch_once_t pred;
    static Instance *obj = nil;
    dispatch_once(&pred, ^{
        obj = [[Instance alloc] init];
    });
    return obj;
}
原文地址:https://www.cnblogs.com/WJJ-Dream/p/5794690.html