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

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

+ (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)
Ty80

http://www.jianshu.com/p/08b1...
看看这篇我的写博客.

黄舟

又去stackovweflow找了下方法,我觉得既然是单例模式,调用者就应该严格按照单例的要求,通过统一的接口(这里是sharedInstance)去创建单例,而不应该出现调用[[class alloc] init]也能成功创建单例的情况,如果出现[[class alloc] init]的情况,我觉得更应该让Xcode给出警告不能用此方法

- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
PHPzhong

额外创建有很多,还有new方法也可以,把这些方面都重载一遍返回 sharedManager 实例,或者直接抛出异常

迷茫

覆盖allocWithZone和copyWithZone方法。
因为通过alloc或者copy还是new,都是通过调用allocWithzone和copyWithzone来分配空间的。
你可以把sharedManager 方法里面的代码写到这两个方法里面,就可以从根本实现了单例的情况

大家讲道理
  • (instancetype)init {
    @throw [NSException异常WithName:@"禁用"原因:@"请使用init代替..." userInfo:nil];
    return self;
    }

Peter_Zhu

这样写就可以了

static Singleton *slt = nil;

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

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
   static dispatch_once_t onceToken;
   dispatch_once(&onceToken, ^{
       slt = [super allocWithZone:zone];
      
   });
   return slt;
}

- (id)copyWithZone:(NSZone *)zone
{
   return slt;
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!