iOS线程安全的单例实现

常规写法

#import <Foundation/Foundation.h>

@interface JeySingletonTest : NSObject

+ (instancetype) shareInstance;

@end
#import "JeySingletonTest.h"

@implementation JeySingletonTest

static JeySingletonTest *_instance = nil;

+ (instancetype) shareInstance
{
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init] ;
    }) ;
    
    return _instance ;
}

@end
    JeySingletonTest* test_obj1 = [JeySingletonTest shareInstance] ;
    NSLog(@"test_obj1 = %@", test_obj1) ;
    JeySingletonTest* test_obj2 = [JeySingletonTest shareInstance] ;
    NSLog(@"test_obj2 = %@", test_obj2) ;
    JeySingletonTest* test_obj3 = [[JeySingletonTest alloc] init] ;
    NSLog(@"test_obj3 = %@", test_obj3) ;
输出:
  2018-12-02 16:02:51.996393+0800 001--Block[1351:40278] test_obj1 = <JeySingletonTest: 0x60000001a400>
2018-12-02 16:02:51.996561+0800 001--Block[1351:40278] test_obj2 = <JeySingletonTest: 0x60000001a400>
2018-12-02 16:02:51.996698+0800 001--Block[1351:40278] test_obj3 = <JeySingletonTest: 0x60400001a800>

看到init出来的对象内存地址不一样了

安全写法

创建对象的步骤分为申请内存(alloc)、初始化(init)这两个步骤,我们要确保对象的唯一性,因此在第一步这个阶段我们就要拦截它。当我们调用alloc方法时,oc内部会调用allocWithZone这个方法来申请内存,我们覆写这个方法,然后在这个方法中调用shareInstance方法返回单例对象,这样就可以达到我们的目的。拷贝对象也是同样的原理,覆写copyWithZone方法,然后在这个方法中调用shareInstance方法返回单例对象。

#import <Foundation/Foundation.h>

@interface JeySingleton : NSObject

+ (instancetype) shareInstance;

@end
#import "JeySingleton.h"

@implementation JeySingleton

static JeySingleton* _instance = nil;

+ (instancetype) shareInstance
{
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        _instance = [[super allocWithZone:NULL] init] ;
    }) ;
    
    return _instance ;
}

+ (id) allocWithZone:(struct _NSZone *)zone
{
    return [JeySingleton shareInstance] ;
}

- (id) copyWithZone:(struct _NSZone *)zone
{
    return [JeySingleton shareInstance] ;
}

@end
    JeySingleton* obj1 = [JeySingleton shareInstance] ;
    NSLog(@"obj1 = %@", obj1) ;
    JeySingleton* obj2 = [JeySingleton shareInstance] ;
    NSLog(@"obj2 = %@", obj2) ;
    JeySingleton* obj3 = [[JeySingleton alloc] init] ;
    NSLog(@"obj3 = %@", obj3) ;
输出:
2018-12-02 16:02:51.995880+0800 001--Block[1351:40278] obj1 = <JeySingleton: 0x60400001a820>
2018-12-02 16:02:51.996067+0800 001--Block[1351:40278] obj2 = <JeySingleton: 0x60400001a820>
2018-12-02 16:02:51.996211+0800 001--Block[1351:40278] obj3 = <JeySingleton: 0x60400001a820>

init出来的对象还是一样的内存地址

参考://www.greatytc.com/p/a0bc8d6525b7

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容