首页 > 其他 > 详细

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

时间:2016-04-22 14:45:20      阅读:184      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!