iOS-简化单例模式(定义成宏 以后通用)

// .h文件
#define HMSingletonH + (instancetype)sharedInstance;

// .m文件
#define HMSingletonM \
static id _instance; \
 \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        _instance = [super allocWithZone:zone]; \
    }); \
    return _instance; \
} \
 \
+ (instancetype)sharedInstance \
{ \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        _instance = [[self alloc] init]; \
    }); \
    return _instance; \
} \
 \
- (id)copyWithZone:(NSZone *)zone \
{ \
    return _instance; \
}

 

posted @ 2015-10-03 14:48  微博和csdn还有你  阅读(204)  评论(0编辑  收藏  举报