#import "CZTool.h"
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (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 : NSObject
CZSingleInterface(Tool)
@end
#import "CZTool.h"
@implementation CZTool
CZSingleImplementation(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;\
}