美文网首页
单例 - 宏

单例 - 宏

作者: 小盒盒 | 来源:发表于2016-03-26 19:02 被阅读14次
    
    // .h文件
    #define WhSingletonH(name) + (instancetype)shared##name;
    
    // .m文件
    #define WhSingletonM(name) \
    static id _instance; \
     \
    + (instancetype)allocWithZone:(struct _NSZone *)zone \
    { \
        static dispatch_once_t onceToken; \
        dispatch_once(&onceToken, ^{ \
            _instance = [super allocWithZone:zone]; \
        }); \
        return _instance; \
    } \
     \
    + (instancetype)shared##name \
    { \
        static dispatch_once_t onceToken; \
        dispatch_once(&onceToken, ^{ \
            _instance = [[self alloc] init]; \
        }); \
        return _instance; \
    } \
     \
    - (id)copyWithZone:(NSZone *)zone \
    { \
        return _instance; \
    }
    

    相关文章

      网友评论

          本文标题:单例 - 宏

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