使用设计模式中的Singleton单例模式来开发iOS应用程序

2020-01-14 22:07:45于海丽

    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}
 
- (id)copyWithZone:(NSZone *)zone
{
    return _instance;
}


使用方法
复制代码
//.h类
//引入这个宏文件
#import "WMSingleton.h"
@interface WMObject : NSObject
WMSingletonH(object)
@end
//.m类

 

@implementation WMObject
WMSingletonM(Car)
@end