当子类化 NSCoder 时,我是否还需要子类化集合类(即:NSArray、NSDictionary)?

发布于 2024-12-18 07:46:59 字数 1337 浏览 4 评论 0原文

我正在探索子类化的想法 NSCoder 读取/写入专有文件格式。我开始相信这可能还需要我对 NSArrayNSDictionary 进行子类化以覆盖 encodeWithCoder: 以确保它们正确编码:

- (void)encodeWithCoder:(NSCoder *)encoder {
    if ([encoder isKindOfClass:[CustomCoder class]]) {
        // call [encoder encode...] as I see fit.
    }else
        [super encodeWithCoder:encoder];
}

我'我不愿意这样做,因为 NSCoder 及其相关协议 NSCoding 应该是自给自足的......要求遵循协议的子类化似乎是糟糕的设计。我是不是理解错了或者想太多了?!?

详细说明一下:
例如,如果文件格式规定项目列表的编码方式如下:

...      // rest of file
0xA1     // marks the beginning of a list
0x0010   // amount of item in list.  The list contains 16 items in this example.
...      // encoded items follows

我想对数组进行编码,它的 encodeWithCoder: 需要如下所示:

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeValueOfObjCType:@encode(short) at:&itemCount];
    for ( /*for each item of the array*/ ) {
        [item encodeWithCoder:encoder];
    }
}

因为我忽略了 NSArray 的 encodeWithCoder: 是实现,我想我必须重写它才能正确使用我的 NSCoder 类。

I'm exploring the idea of subclassing NSCoder to read/write a proprietary file format. I'm starting to believe this might also require me to subclass NSArray and NSDictionary to override encodeWithCoder: to ensure they are encoded properly:

- (void)encodeWithCoder:(NSCoder *)encoder {
    if ([encoder isKindOfClass:[CustomCoder class]]) {
        // call [encoder encode...] as I see fit.
    }else
        [super encodeWithCoder:encoder];
}

I'm reluctant to do this because NSCoder and its related protocols NSCoding should be self-sufficient... Requiring subclassing classes following the protocol seems like bad design. Am I missunderstanding or over-thinking something?!?

Elaborating a little:
For example, if the file format stipulates that lists of items are encoded like so:

...      // rest of file
0xA1     // marks the beginning of a list
0x0010   // amount of item in list.  The list contains 16 items in this example.
...      // encoded items follows

I would imagine that to encode an array, it's encodeWithCoder: would need to look like this:

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeValueOfObjCType:@encode(short) at:&itemCount];
    for ( /*for each item of the array*/ ) {
        [item encodeWithCoder:encoder];
    }
}

Because I ignore how NSArray's encodeWithCoder: is implement, I imagine I'd have to override it to use my NSCoder class properly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生来就爱笑 2024-12-25 07:46:59

我不明白为什么你认为你需要子类化集合类。您的实现与默认实现有何不同?

在某些时候,您的 NSCoder 子类的 encodeObject: 方法将被调用。在该方法中,您可以识别 NSArray 并根据您的格式要求对其进行编码。我的意思是,由您的 NSCoder 子类调用它遇到的对象的encodeWithCoder: 或不以其他方式调用和编码它们。

I don't understand why do you think you need to subclass collection classes. How your implementation will differ from default one?

At some point encodeObject: method of your NSCoder subclass will get called. In that method you can recognize NSArray and encode it as your format requires. I mean it's up to your NSCoder subclass to call encodeWithCoder: of objects it encounters or not to call and encode them somehow else.

紙鸢 2024-12-25 07:46:59

幸运的是,您不需要子类化任何集合类,例如 NSArrayNSDictionary。这是因为多态性的魔力,即集合将通过调用 encodeWithCoder: 对每个对象进行单独编码。因此,只需在类上实现 NSCoder 协议就足以对可能包含该类实例的集合进行编码。

Luckily for you, you will not need to subclass any collection classes such as NSArray or NSDictionary. This is because of the magic of polymorphism, the idea that the collection will encode each object individually by calling encodeWithCoder: on it. Because of this, simply implementing the NSCoder protocol on a class will be sufficient for encoding a collection that may contain instances of that class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文