当子类化 NSCoder 时,我是否还需要子类化集合类(即:NSArray、NSDictionary)?
我正在探索子类化的想法 NSCoder
读取/写入专有文件格式。我开始相信这可能还需要我对 NSArray
和 NSDictionary
进行子类化以覆盖 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不明白为什么你认为你需要子类化集合类。您的实现与默认实现有何不同?
在某些时候,您的
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 yourNSCoder
subclass will get called. In that method you can recognizeNSArray
and encode it as your format requires. I mean it's up to yourNSCoder
subclass to callencodeWithCoder:
of objects it encounters or not to call and encode them somehow else.幸运的是,您不需要子类化任何集合类,例如
NSArray
或NSDictionary
。这是因为多态性的魔力,即集合将通过调用encodeWithCoder:
对每个对象进行单独编码。因此,只需在类上实现 NSCoder 协议就足以对可能包含该类实例的集合进行编码。Luckily for you, you will not need to subclass any collection classes such as
NSArray
orNSDictionary
. This is because of the magic of polymorphism, the idea that the collection will encode each object individually by callingencodeWithCoder:
on it. Because of this, simply implementing theNSCoder
protocol on a class will be sufficient for encoding a collection that may contain instances of that class.