美文网首页
代码快写

代码快写

作者: WSWshallwe | 来源:发表于2019-04-22 11:32 被阅读0次

    单例 定义宏

    #define DJ_SINGLETON_DEF(_type_) + (_type_ *)sharedInstance;\

    +(instancetype) alloc __attribute__((unavailable("call sharedInstance instead")));\

    +(instancetype) new __attribute__((unavailable("call sharedInstance instead")));\

    -(instancetype) copy __attribute__((unavailable("call sharedInstance instead")));\

    -(instancetype) mutableCopy __attribute__((unavailable("call sharedInstance instead")));\

    #define DJ_SINGLETON_IMP(_type_) + (_type_ *)sharedInstance{\

    static _type_ *theSharedInstance = nil;\

    static dispatch_once_t onceToken;\

    dispatch_once(&onceToken, ^{\

    theSharedInstance = [[super alloc] init];\

    });\

    return theSharedInstance;\

    }

    定义和实现:

    @interface DJSingleton : NSObject

        DJ_SINGLETON_DEF(DJSingleton);

    @end

    @implementation DJSingleton

        DJ_SINGLETON_IMP(DJSingleton);

    @end

    相关文章

      网友评论

          本文标题:代码快写

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