ARC版的模版宏的写法和用法
2015-01-05 15:24
176 查看
ARCSingletonTemplate.h #define SYNTHESIZE_SINGLETON_FOR_HEADER(className) \ \ + (className *)shared##className; #define SYNTHESIZE_SINGLETON_FOR_CLASS(className) \ \ + (className *)shared##className { \ static className *shared##className = nil; \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ shared##className = [[self alloc] init]; \ }); \ return shared##className; \ } 基本是使用了 GCD中的dispatch_once接收一个在应用生命周期只会被调用一次的代码块,而且它还是线程安全的
用法
AppPreference.h#import <Foundation/Foundation.h>
#import "ARCSingletonTemplate.h"
@interface AppPreference :NSObject
//使用宏模版生成单例所需要的code
SYNTHESIZE_SINGLETON_FOR_HEADER(AppPreference)
@end
AppPreference.m
#import "AppPreference.h"
@implementation AppPreference
//使用宏模版生成单例所需要的code
SYNTHESIZE_SINGLETON_FOR_CLASS(AppPreference)
//例子
- (void)sample{
AppPreference* appPreference = [AppPreferencesharedAppPreference];
}
@end
使用 shareClassName 就可以获取实例。
相关文章推荐
- C++模版STL中 map 和 string, vector 的用法详解!
- 模版 template 的用法
- hibernate的like用法以及hibernate.query.factory_class的写法问题
- js模版引擎handlebars.js实用教程——if-判断的基本用法
- (转)pipe row的用法, Oracle split 函数写法.
- SQL语句的写法:update语句中 case...when 用法
- CSS Hack 浏览器兼容写法 用法
- pipe row 的用法,Oracle split 函数写法
- thinkphp 中 __APP__等模版替换的用法
- linux中stl模版的用法1
- 模版单例模式写法
- [转]pipe row的用法, Oracle split 函数写法
- 通过 DAOFactory 的泛型写法来演示Java泛型的特殊用法
- java的list的几种排序写法整理(sort的用法)
- C++模版STL中 map 和 string, vector 的用法详解!
- js组件写法模版(一)
- GridView的用法、自定义控件入门写法和界面切换动画
- hibernate的like用法以及hibernate.query.factory_class的写法问题
- pattern 的用法都一样,这里不再啰嗦各种详细写法了,只是列出来一些常用的正则就好了:
- GridView的用法、自定义控件入门写法和界面切换动画