代码快写

单例 定义宏

#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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 我们常常会听说 Objective-C 是一门动态语言,那么这个「动态」表现在哪呢?我想最主要的表现就是 Obje...
    Ethan_Struggle阅读 2,236评论 0 7
  • 如题 效果如下: @interface AAA : NSObject <MySingleton>@end 单例模式...
    congratulas阅读 496评论 0 1
  • 本文转载自:http://yulingtianxia.com/blog/2014/11/05/objective-...
    ant_flex阅读 808评论 0 1
  • 转载:http://yulingtianxia.com/blog/2014/11/05/objective-c-r...
    F麦子阅读 775评论 0 2
  • 我与这位朋友在大学期间有大概五次相识的机会,可直到最后一次我还不知道他的名字,或是其他经历。和千千万万擦肩而过的行...
    氵冫丶阅读 380评论 0 2