如何更改NSKeyedArchiver中编码的对象的层次结构?(How to change hierarchy of objects encoded in NSKeyedArchiver?)

编程入门 行业动态 更新时间:2024-10-25 22:24:41
如何更改NSKeyedArchiver中编码的对象的层次结构?(How to change hierarchy of objects encoded in NSKeyedArchiver?)

我们已经发布了一个游戏,它在整个课程中使用NSCoding保存/加载功能。 我们现在正在研究下一个版本,但在我们这样做之前,我们需要重构代码,以便它具有更好的性能,并且更容易实现未来的功能(这往往会发生很大的变化)。

问题是在审查新设计之后。 它具有与先前版本不同的NSCoding层次结构。

v1.0 GameSavedFile(使用NSKeyedArchiver) - > UserData - > User(UserData的父) - >一堆对象和原语

v1.1用户(使用NSKeyedArchiver) - >一堆对象和基元

v1.1具有更简单,更直接的结构。 在v1.0中,UserData是User的子类,而在v1.1中,我们只对所有User数据使用一个类。

这种方式更简单,更实用。 但是,如何在不破坏任何内容的情况下将所有v1.0数据移植到v1.1? 我已经读过有一个setClassName:forClass:方法,但我认为只替换新类的旧类。

我能想到的最干净,最安全的方法是检查游戏是否仍在使用旧版本(v1.0)。 如果是这样,将NSCoding层次结构转换为plist / dictionary,然后在v1.1代码中将其分散。

我对任何可能的建议/解决方案持开放态度,但我更喜欢基础框架中已有的解决方案(如果可能的话)。

谢谢。

We already have a game published that has save/load feature using NSCoding throughout the classes. We're now working on the next version but before we do that we need to refactor the code so it'll have better performance and it'll be easier to implement future features (this tends to get changed a lot).

The problem is after reviewing the new design. It has a different NSCoding hierarchy than the previous version.

v1.0 GameSavedFile (using NSKeyedArchiver) -> UserData -> User (parent of UserData) -> bunch of objects and primitives

v1.1 User (using NSKeyedArchiver) -> bunch of objects and primitives

v1.1 has a simpler and more straightforward structure. In v1.0, UserData is a subclass of User while in v1.1 we'll only use one class for all the User data.

It's easier and more practical this way. However, how can I port all v1.0 data to v1.1 without breaking anything? I've read that there's a setClassName:forClass: method but I think that only replaces the old class for the new class.

The cleanest and safest way I could think of is to check if the game is still using the old version (v1.0). If it does, convert the NSCoding hierarchy to a plist/dictionary then scatter it around in v1.1 code.

I am open for any possible suggestion/solution but I'll prefer a solution that's already available in the Foundation framework (if that's possible).

Thanks.

最满意答案

当然,您应该阅读Keyed Archives的前向和后向兼容性 ,但它并没有真正解决您的问题。 (无论如何都要读。)

假设你还没有发货v1.1,这是我的建议:

为v1.1创建一个新的保存文件名。 我经常在文件名中包含版本号以简化此操作。 这样可以在打开之前轻松判断您尝试阅读的版本。 我通常比Apple建议将该版本存储在文件中更好。 (当然,如果更改文件名会导致用户混淆,或者用户可以控制文件名,那么您当然应该将该版本存储在文件中。)

对于v1.0文件,创建只保存原始数据的V10GameSaveFile类。 然后使用setClassName:forClass:将序列化对象移动到虚拟类类。

现在您已将结构存储在内存中,将其转换为新的对象模型,重新保存并删除旧文件。

Of course you should read Forward and Backward Compatibility for Keyed Archives, but it doesn't really address your issues here. (Read it anyway.)

Assuming you have not shipped v1.1 yet, here is my recommendation:

Create a new save filename for v1.1. I often include the version number in the filename to simplify this. This makes it easy to tell which version you're trying to read before you open it. I generally like this better than Apple's recommendation of storing the version in the file. (Of course if changing the filename creates user confusion, or the user has control over the filename, then you should of course store the version in the file.)

For v1.0 files, create classes like V10GameSaveFile that just hold raw data. Then use setClassName:forClass: to move the serialized objects to the dummy classes classes.

Now that you have your structure in memory, convert it to the new object model, re-save, and delete the old file.

更多推荐

本文发布于:2023-08-04 07:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1413050.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何更改   层次   对象   结构   NSKeyedArchiver

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!