1.arc与非arc互相转换
直接在targets->build phases中修改compiler Flags,是否支持arc。添加:-fobjc-arc,就可以让旧项目支持arc。如果想让原来支持arc的不使用arc则添加-fno-objc-arc
2.arc下使用单例 (引用)
http://jakend.iteye.com/blog/1842107
大家知道,单例模式是ios里面经常使用的模式,例如
[UIApplicationsharedApplication] (获取当前应用程序对象)、[UIDevicecurrentDevice](获取当前设备对象);
单例模式的写法也很多。
第一种:
- static Singleton *singleton = nil;
- // 非线程安全,也是最简单的实现
- + (Singleton *)sharedInstance
- {
- if (!singleton) {
- // 这里调用alloc方法会进入下面的allocWithZone方法
- singleton = [[self alloc] init];
- }
- return singleton;
- }
- // 这里重写allocWithZone主要防止[[Singleton alloc] init]这种方式调用多次会返回多个对象
- + (id)allocWithZone:(NSZone *)zone
- {
- if (!singleton) {
- NSLog(@"进入allocWithZone方法了...");
- singleton = [super allocWithZone:zone];
- return singleton;
- }
- return nil;
- }
第二种:
- // 加入线程安全,防止多线程情况下创建多个实例
- + (Singleton *)sharedInstance
- {
- @synchronized(self)
- {
- if (!singleton) {
- // 这里调用alloc方法会进入下面的allocWithZone方法
- singleton = [[self alloc] init];
- }
- }
- return singleton;
- }
- // 这里重写allocWithZone主要防止[[Singleton alloc] init]这种方式调用多次会返回多个对象
- + (id)allocWithZone:(NSZone *)zone
- {
- // 加入线程安全,防止多个线程创建多个实例
- @synchronized(self)
- {
- if (!singleton) {
- NSLog(@"进入allocWithZone方法了...");
- singleton = [super allocWithZone:zone];
- return singleton;
- }
- }
- return nil;
- }
第三种:
- __strong static Singleton *singleton = nil;
- // 这里使用的是ARC下的单例模式
- + (Singleton *)sharedInstance
- {
- // dispatch_once不仅意味着代码仅会被运行一次,而且还是线程安全的
- static dispatch_once_t pred = 0;
- dispatch_once(&pred, ^{
- singleton = [[super allocWithZone:NULL] init];
- });
- return singleton;
- }
- // 这里
- + (id)allocWithZone:(NSZone *)zone
- {
- /* 这段代码无法使用, 那么我们如何解决alloc方式呢?
- dispatch_once(&pred, ^{
- singleton = [super allocWithZone:zone];
- return singleton;
- });
- */
- return [self sharedInstance];
- }
- - (id)copyWithZone:(NSZone *)zone
- {
- return self;
- }
第三种是ARC下单例模式,也是比较方便的, 但是[[Singleton alloc] init];这种情况下就可以生成多个对象了,怎么办呢,我们暂时先使用计数的方式来防止多次创建实例,如果大家有更好的方法,可以留言给我。谢谢~
我们采用直接返回sharedInstance的方法进行创建。
补充:使用dispatch_once,这个方法的意思是在应用程序生命周期内,这方法只执行一次,这就是ARC下结合GCD的一个单例的代码
3.使用单例的宏定义模式
因为我们可能需要多个单例的类,每个都写一次比较麻烦,可以更方便一些,把这些代码写成宏的形式,只需要传入类名就可以创建一个单例的类了。
http://blog.youkuaiyun.com/totogo2010/article/details/8373642
- #define DEFINE_SINGLETON_FOR_HEADER(className) \
- \
- + (className *)shared##className;
- #define DEFINE_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; \
- }
- #import <Foundation/Foundation.h>
- @interface testSingleton : NSObject
- DEFINE_SINGLETON_FOR_HEADER(testSingleton);
- @end
获取单例:
- - (void)test
- {
- testSingleton *testShare = [testSingleton sharedtestSingleton];
- }