单例对象在归档再解档以后是否是同一个对象

2019-04-18  本文已影响0人  格雷s

1.在重写了allocWithZone方法后,归档前后是同一个对象,否则不是,解档回调用allocWithZone:方法

+ (instancetype)sharedSingleton {
    static BaseModel *_sharedSingleton = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //不能再使用alloc方法
        //因为已经重写了allocWithZone方法,所以这里要调用父类的分配空间的方法
        _sharedSingleton = [[super allocWithZone:NULL] init];
    });
    return _sharedSingleton;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    return [BaseModel sharedSingleton];
}
上一篇 下一篇

猜你喜欢

热点阅读