iOS单例模式代码示例

#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单例模式代码示例)