//
// QGSingleton.h
// 单例联系
//
// Created by 李超群 on 16/6/12.
// Copyright © 2016年 李超群. All rights reserved.
//
/** 单例对外的类方法 */
#define QGSingletonH + (instancetype)sharedInstance;
/** 单例类方法的实现 */
#define QGSingletonM \
static id g_instance; \
\
+ (instancetype)sharedInstance{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
g_instance = [[self alloc]init]; \
}); \
return g_instance; \
} \
\
\
+(instancetype)allocWithZone:(struct _NSZone *)zone{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
g_instance = [super allocWithZone:zone]; \
}); \
return g_instance; \
} \
\
\
- (id)copyWithZone:(NSZone *)zone{ \
return g_instance; \
// QGSingleton.h
// 单例联系
//
// Created by 李超群 on 16/6/12.
// Copyright © 2016年 李超群. All rights reserved.
//
/** 单例对外的类方法 */
#define QGSingletonH + (instancetype)sharedInstance;
/** 单例类方法的实现 */
#define QGSingletonM \
static id g_instance; \
\
+ (instancetype)sharedInstance{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
g_instance = [[self alloc]init]; \
}); \
return g_instance; \
} \
\
\
+(instancetype)allocWithZone:(struct _NSZone *)zone{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
g_instance = [super allocWithZone:zone]; \
}); \
return g_instance; \
} \
\
\
- (id)copyWithZone:(NSZone *)zone{ \
return g_instance; \
}
调用时候就是以下的方法
.h中
#import <UIKit/UIKit.h>
#import "QGSingleton.h"
@interface ViewController : UIViewController
QGSingletonH
@end
.m 中
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
QGSingletonM
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end