OC单例宏写法

来源:互联网 发布:狸窝ppt转换器 mac 编辑:程序博客网 时间:2024/04/27 23:19

//.h文件

#define TGSingletonH(name) + (instancetype)share##name;


//.m文件

#define TGSingletonM(name)\

static id _instance = nil;\

+ (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;\

}

0 0
原创粉丝点击