单例正确的写法

#import 

NS_ASSUME_NONNULL_BEGIN

@interface H5Container : NSObject
+ (instancetype)sharedInstance;
- (instancetype)init NS_UNAVAILABLE;
@end

NS_ASSUME_NONNULL_END
#import "H5Container.h"
@interface H5Container ()
@end
@implementation H5Container
+ (instancetype)sharedInstance {
    
    static H5Container *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[super allocWithZone:NULL] _init];

    });
    return manager;
}
- (instancetype)_init {
    return [super init];
}
+ (id)allocWithZone:(struct _NSZone *)zone {
    return [self sharedInstance];
}
- (id)copyWithZone:(nullable NSZone*)zone {
    return self;
}
@end

你可能感兴趣的:(单例正确的写法)