美文网首页
单例正确的写法

单例正确的写法

作者: 御雪飞斐 | 来源:发表于2019-03-26 15:13 被阅读0次
    #import <Foundation/Foundation.h>
    
    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
    

    相关文章

      网友评论

          本文标题:单例正确的写法

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