保存并加载单例

Joelfischerr

我创建了一个这样的单例:

+ (instancetype)sharedDataStore {

static SSDataStore *_sharedDataStore = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    _sharedDataStore = [[self alloc] init];
});

return _sharedDataStore;
}

将其保存到document文件夹或从中加载的最佳方法是什么?我尝试使用NSKeyedArchiver保存它,但是之后我不知道如何加载它。我该怎么办而不是

 _sharedDataStore = [[self alloc] init];

约翰·托普利

要恢复单身人士的状态,您需要initWithCoderNSCoding协议中实现该方法就像是:

- (id)initWithCoder:(NSCoder *)decoder {
    self = [super init];
    if (!self) {
        return nil;
    }

    self.property1 = [decoder decodeObjectForKey:@"property1"];
    self.property2 = [decoder decodeObjectForKey:@"property2"];

    return self;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章