objective-c - 如何避免通过[[alloc] init]创建iOS单例类
大家讲道理
大家讲道理 2017-04-18 09:41:01
0
6
650

网站普遍的创建单例类的方法有下面两种:

+ (instancetype)sharedManager {
    static id _sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[self alloc] init];
    });
    return _sharedInstance;
}
+ (instancetype)sharedManager {
    static id _sharedInstance = nil;
    @synchronized(self) {
        if (_sharedInstance == nil)
            _sharedInstance = [[self alloc] init];
    }
    return _sharedInstance;
}

但是该如何避免意外的用[[alloc] init]创建呢?主要是发现网上找到的大多仅仅只有上面的代码,少有考虑被init或者copy的情况

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(6)
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!