您的位置:首页 > 移动开发 > IOS开发

[iOS]编写单例的正确方式

2017-03-07 00:00 148 查看
Swift

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

Objective-C

@interface Kraken : NSObject
@end

@implementation Kraken

+ (instancetype)sharedInstance {
static Kraken *sharedInstance = nil;
static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
sharedInstance = [[Kraken alloc] init];
});
return sharedInstance;
}

@end

Swift中编写单例的正确方式
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS Swift Objective-C