码迷,mamicode.com
首页 > 其他好文 > 详细

单例的宏定义实现-07-GCD

时间:2016-04-22 14:46:53      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

 1 //
 2 //  LYHSingleTon.h
 3 //  07-单例的宏定义实现
 4 //
 5 //  Created by mac on 16/4/22.
 6 //  Copyright © 2016年 mac. All rights reserved.
 7 //
 8 
 9 #ifndef LYHSingleTon_h
10 #define LYHSingleTon_h
11 
12 // .h文件
13 
14 #define LYHSingleTonH + (instancetype)sharedInstance;
15 
16 // .m文件
17 
18 #define LYHSingleTonM 19 /**
20  *  1. 创建一个全局变量,作用域比较广泛,所有的文件中都能够访问
21  */22 id _instance;23 24 /**
25  *  2. 重写allocWithZone:只分配一次内存
26  */27 + (instancetype)allocWithZone:(struct _NSZone *)zone {28     29     static dispatch_once_t onceToken;30     dispatch_once(&onceToken, ^{31         _instance = [super allocWithZone:zone];32     });33     return _instance;34 }35 /**
36  *  3. 既保证了单例(只分配一次内存),又保证了只init一次;
37  */38 + (instancetype)sharedInstance {39     40     static dispatch_once_t onceToken;41     dispatch_once(&onceToken, ^{42         _instance = [[self alloc] init];43     });44     return _instance;45 }46 /**
47  *  4. 重写copy方法
48  */49 - (id)copyWithZone:(NSZone *)zone {50     51     return _instance;52 }
53 
54 /**
55  *  下面是不添加注释的宏定义
56  */
57 ///*
58  
59 #define LYHSingleTonM 60 id _instance;61 62 + (instancetype)allocWithZone:(struct _NSZone *)zone {63 64 static dispatch_once_t onceToken;65 dispatch_once(&onceToken, ^{66 _instance = [super allocWithZone:zone];67 });68 return _instance;69 }70 71 + (instancetype)sharedMusicTool {72 73 static dispatch_once_t onceToken;74 dispatch_once(&onceToken, ^{75 _instance = [[self alloc] init];76 });77 return _instance;78 }79 80 - (id)copyWithZone:(NSZone *)zone {81 82 return _instance;83 }
84  
85 */
86 
87 #endif /* LYHSingleTon_h */

 

单例的宏定义实现-07-GCD

标签:

原文地址:http://www.cnblogs.com/foreveriOS/p/5421227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!