工具类:快速创建单例

来源:互联网 发布:psp游戏数据已损坏 编辑:程序博客网 时间:2024/06/06 03:58
#import "CZTool.h"#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.}- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {    NSLog(@"%@",[CZTool shareTool]);    NSLog(@"%@",[[CZTool alloc]init]);    NSLog(@"%@",[[CZTool alloc]init]);}@end
#import <Foundation/Foundation.h>#import "CZSingle.h"@interface CZTool : NSObjectCZSingleInterface(Tool)@end#import "CZTool.h"@implementation CZToolCZSingleImplementation(Tool)@end
创建头文件CZSingle.h// 两个##拼接字符串 把传过来的字符串拼接到##后面去#define CZSingleInterface(name) +(instancetype)share##name;// 反斜杠用来宏定义换行拼接#define CZSingleImplementation(name) + (instancetype)share##name {\return [[self alloc]init];\}\+ (instancetype)allocWithZone:(struct _NSZone *)zone {\    static dispatch_once_t onceToken;\    static id instance;\    dispatch_once(&onceToken, ^{\        instance = [super allocWithZone:zone];\    });\    return instance;\}
0 0
原创粉丝点击