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