您的位置:首页 > 其它

工具类:快速创建单例

2016-04-05 16:12 489 查看
#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 : 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;\
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: