Singleton 头文件

 

//.h文件

#define SingletonH(name) \

+(instancetype) shared##name ;

 

//.m文件

#define SingletonM(name) \

static id instance##name; \

+(instancetype) shared##name { \

    return [[self allocWithZone:nil]init]; \

} \

+(instancetype)allocWithZone:(struct _NSZone *)zone{ \

    static dispatch_once_t onceToken; \

    dispatch_once(&onceToken, ^{ \

        instance##name = [super allocWithZone:zone]; \

    }); \

    return instance##name; \

} \

+(instancetype)alloc { \

    return [self allocWithZone:nil];\

}

posted on 2015-04-25 12:23  雾里寻梦  阅读(222)  评论(0)    收藏  举报

导航