.h文件
#import <Foundation/Foundation.h> @interface HMDataTool : NSObject + (instancetype)sharedDataTool; @end
.m文件
#import "HMDataTool.h" @implementation HMDataTool // 用来保存唯一单例对象 static id _instance; + (instancetype)allocWithZone:(struct _NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [super allocWithZone:zone]; }); return _instance; } + (instancetype)sharedDataTool { // 线程安全的 static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[self alloc] init]; }); return _instance; } - (id)copyWithZone:(NSZone *)zone { return _instance; } @end
时间: 2024-10-31 23:43:27