2019-05-16 星期四 5 of 7

2019-05-16  本文已影响0人  老布威利斯

to combine different things in a way that produces a pleasant result.

a story that blends story and legend.
blend the sugar,eggs and flour

static NSObject *_instance = nil;

锁住alloc方法,锁住init方法,锁住copy方法,才能保证是全局唯一单例

//锁定 alloc方法
+(id)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}
//锁定init方法
-(instancetype)init{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super init];
    });
    return _instance;
}
//锁定copy
- (id)copyWithZone:(NSZone __unused*)zone {
    return _instance;
}
//锁定 mutable copy
- (id)mutableCopyWithZone:(NSZone __unused*)zone {
    return _instance;
}
上一篇 下一篇

猜你喜欢

热点阅读