美文网首页
宏定义单例类

宏定义单例类

作者: 宁梓茞 | 来源:发表于2018-02-10 20:41 被阅读0次
    // .h
    #define singleton_interface(class) + (instancetype)shared##class;
    
    // .m
    #define singleton_implementation(class) \
    static class *_instance; \
    \
    + (id)allocWithZone:(struct _NSZone *)zone \
    { \
        static dispatch_once_t onceToken; \
        dispatch_once(&onceToken, ^{ \
            _instance = [super allocWithZone:zone]; \
        }); \
    \
        return _instance; \
    } \
    \
    + (instancetype)shared##class \
    { \
        if (_instance == nil) { \
            _instance = [[class alloc] init]; \
        } \
    \
        return _instance; \
    }

    相关文章

      网友评论

          本文标题:宏定义单例类

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