使用NSCoding将类保存到objc中的NSUserDefaults

时间:2022-08-24 16:24:10

I am a little confused as to how i should proceed here, say this is my class

关于我应该如何继续这里,我有点困惑,说这是我的班级

@interface PlayerStats : NSObject <NSCoding>
{
    NSMutableDictionary *gameStats;
    NSUserDefaults *prefs;

    GamePlayStats *_gamePlayStats;

    GameStats *_openGameStats;
    GameStats *_duelGameStats;
    GameStats *_challengeGameStats;

    float _averagePanelsRevealed;
    float _averageGuessSpeed;

    int _totalCoinsEarned;
    int _totalTokensEarned;

    int _playerLevel;
}

now when i want to save the class i wish to do it this way as it seems more sensible to just save the class and when i get it back i can just call it's variables though it's methods etc , etc.

现在,当我想保存这个类时,我希望这样做,因为它似乎更加明智地保存类,当我得到它时,我可以调用它的变量,虽然它的方法等等。

- (void)encodeWithCoder:(NSCoder *)encoder 
{
    [encoder encodeObject:self forKey:@"playerStats"];
}

However all the examples i have seen require you to load all the variables in individually so it should looke something more like

但是我所看到的所有例子都要求你单独加载所有变量,所以它应该更喜欢的东西

- (void)encodeWithCoder:(NSCoder *)encoder 
{
    [encoder encodeObject:self.gamePlayStats forKey:@"gamplaystats"];
    [encoder encodeObject:self._openGameStats forKey:@"openGameStats"];
    etc..
}

is there a reason for why this is the case?

为什么会出现这种情况?

cheers

干杯

2 个解决方案

#1


1  

The function - (void)encodeWithCoder:(NSCoder *)encoder ask you to decide which variable you want to save to the encoder. The information encoder generates for you is the class information. Inside [encoder encodeObject:aObject forKey:@"key"], it will try to call the [aObject encodeWithCoder:encoder]. I think if write encodeWithCoder: like

函数 - (void)encodeWithCoder:(NSCoder *)编码器要求您决定要将哪个变量保存到编码器。信息编码器为您生成的是班级信息。在[encoder encodeObject:aObject forKey:@“key”]里面,它会尝试调用[aObject encodeWithCoder:encoder]。我想如果写encodeWithCoder:喜欢

- (void)encodeWithCoder:(NSCoder *)encoder 
{
    [encoder encodeObject:self forKey:@"playerStats"];
}

Will write nothing but your class's name and class type, because apple has enabled the reference checking in the coder, it won't encode the same object twice. Otherwise the encodeWithCoder on the same object will be called many times.

除了你的类的名称和类类型之外什么都不会写,因为apple已经启用了编码器中的引用检查,它不会对同一个对象进行两次编码。否则,将多次调用同一对象上的encodeWithCoder。

Therefore, you have to encode necessary properties/variables into the encoder to save the class's data.

因此,您必须将必要的属性/变量编码到编码器中以保存类的数据。

#2


0  

Here is an extract from Apple's doc on NSCoding protocol: In keeping with object-oriented design principles, an object being encoded or decoded is responsible for encoding and decoding its instance variables. You can read the full text on NSCoding.

以下是Apple的NSCoding协议文档的摘录:为了与面向对象的设计原则保持一致,正在编码或解码的对象负责编码和解码其实例变量。您可以阅读NSCoding的全文。

For your sample snippet, you need to follow the latter one. Also, the GamePlayStats and
GameStats classes also needed to conform to be NSCoding protocol as well.

对于您的示例代码段,您需要遵循后者。此外,GamePlayStats和GameStats类也需要符合NSCoding协议。

#1


1  

The function - (void)encodeWithCoder:(NSCoder *)encoder ask you to decide which variable you want to save to the encoder. The information encoder generates for you is the class information. Inside [encoder encodeObject:aObject forKey:@"key"], it will try to call the [aObject encodeWithCoder:encoder]. I think if write encodeWithCoder: like

函数 - (void)encodeWithCoder:(NSCoder *)编码器要求您决定要将哪个变量保存到编码器。信息编码器为您生成的是班级信息。在[encoder encodeObject:aObject forKey:@“key”]里面,它会尝试调用[aObject encodeWithCoder:encoder]。我想如果写encodeWithCoder:喜欢

- (void)encodeWithCoder:(NSCoder *)encoder 
{
    [encoder encodeObject:self forKey:@"playerStats"];
}

Will write nothing but your class's name and class type, because apple has enabled the reference checking in the coder, it won't encode the same object twice. Otherwise the encodeWithCoder on the same object will be called many times.

除了你的类的名称和类类型之外什么都不会写,因为apple已经启用了编码器中的引用检查,它不会对同一个对象进行两次编码。否则,将多次调用同一对象上的encodeWithCoder。

Therefore, you have to encode necessary properties/variables into the encoder to save the class's data.

因此,您必须将必要的属性/变量编码到编码器中以保存类的数据。

#2


0  

Here is an extract from Apple's doc on NSCoding protocol: In keeping with object-oriented design principles, an object being encoded or decoded is responsible for encoding and decoding its instance variables. You can read the full text on NSCoding.

以下是Apple的NSCoding协议文档的摘录:为了与面向对象的设计原则保持一致,正在编码或解码的对象负责编码和解码其实例变量。您可以阅读NSCoding的全文。

For your sample snippet, you need to follow the latter one. Also, the GamePlayStats and
GameStats classes also needed to conform to be NSCoding protocol as well.

对于您的示例代码段,您需要遵循后者。此外,GamePlayStats和GameStats类也需要符合NSCoding协议。