OC和Swift的单例模式

OC版的单例模式

// OC版
+ (instancetype)sharedInstance
{
    static id sharedInstance = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        sharedInstance = [[self alloc] init];
    });

    return sharedInstance;
}

Swift的单例模式

lass TheOneAndOnlyKraken {
    static let sharedInstance = TheOneAndOnlyKraken()
    private init() {} //This prevents others from using the default '()' initializer for this class.
}

这地方就不做过多的解释了,想细的请挫下面的链接
http://www.cocoachina.com/swift/20151207/14584.html

你可能感兴趣的:(OC和Swift的单例模式)