美文网首页
iOS单例模式代码示例

iOS单例模式代码示例

作者: 搬砖人666 | 来源:发表于2020-11-11 14:59 被阅读0次
#import "XXXClass.h"

@implementation XXXClass

+ (instancetype)shareInstance {
    static XXXClass *_sharedSingleton = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedSingleton = [[super allocWithZone:NULL] init];
    });
    return _sharedSingleton;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    return [XXXClass shareInstance];
}
//如有需要,重写copy

@end

相关文章

网友评论

      本文标题:iOS单例模式代码示例

      本文链接:https://www.haomeiwen.com/subject/ymiebktx.html