在自定义类的子类上使用 NSCoding
我正在使用 NSCoding 来归档/取消归档自定义类作为数据持久性的方法。如果该对象是 NSObject 的子类,那么它工作得很好,但我也有作为自定义对象的子类的对象。我是否需要更改 initWithCoder: 方法以及encodeWithCoder?现在,特定于子类的属性可以很好地编码/解码,但子类从超类继承的属性则不能。有什么想法吗?这是基本结构:
@interface NewsItem : NSObject <NSCoding, NSCopying> {
//properties for the super class here
}
@implementation NewsItem
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:itemName forKey:kItemNameKey];
//etc etc
}
- (id)initWithCoder:(NSCoder *)coder {
if ( (self = [super init]) )
{
self.itemName = [coder decodeObjectForKey:kItemNameKey];
//etc etc
}
return self;
}
-(id)copyWithZone:(NSZone *)zone {
NewsItem *copy = [[[self class] allocWithZone: zone] init];
copy.itemName = [[self.itemName copy] autorelease];
//etc etc
return copy;
}
和子类:
@interface File : NewsItem {
NSString *fileSizeString;
//etc etc
}
@implementation File
- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder]; //added this, but didn't seem to make a difference
[coder encodeObject:fileSizeString forKey:kFileSizeStringKey];
//etc etc
}
- (id)initWithCoder:(NSCoder *)coder {
if ( (self = [super init]) )
{
self.fileSizeString = [coder decodeObjectForKey:kFileSizeStringKey];
//etc etc
}
return self;
}
-(id)copyWithZone:(NSZone *)zone {
File *copy = (File *)[super copyWithZone:zone];
copy.fileSizeString = [[self.fileSizeString copy] autorelease];
//etc etc
return copy;
}
I am using NSCoding to archive/unarchive a custom class as a method of data persistence. It works fine if the object is a subclass of NSObject, but I also have objects that are subclasses of custom objects. Do I need to change the initWithCoder: method as well as encodeWithCoder? Right now, the properties that are specific to the subclass encode/decode fine, but properties the subclass inherit from the superclass do not. Any thoughts? Here is the basic structure:
@interface NewsItem : NSObject <NSCoding, NSCopying> {
//properties for the super class here
}
@implementation NewsItem
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:itemName forKey:kItemNameKey];
//etc etc
}
- (id)initWithCoder:(NSCoder *)coder {
if ( (self = [super init]) )
{
self.itemName = [coder decodeObjectForKey:kItemNameKey];
//etc etc
}
return self;
}
-(id)copyWithZone:(NSZone *)zone {
NewsItem *copy = [[[self class] allocWithZone: zone] init];
copy.itemName = [[self.itemName copy] autorelease];
//etc etc
return copy;
}
and the subclass:
@interface File : NewsItem {
NSString *fileSizeString;
//etc etc
}
@implementation File
- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder]; //added this, but didn't seem to make a difference
[coder encodeObject:fileSizeString forKey:kFileSizeStringKey];
//etc etc
}
- (id)initWithCoder:(NSCoder *)coder {
if ( (self = [super init]) )
{
self.fileSizeString = [coder decodeObjectForKey:kFileSizeStringKey];
//etc etc
}
return self;
}
-(id)copyWithZone:(NSZone *)zone {
File *copy = (File *)[super copyWithZone:zone];
copy.fileSizeString = [[self.fileSizeString copy] autorelease];
//etc etc
return copy;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
File
的initWithCoder:
方法中应该是
Inside the
File
'sinitWithCoder:
methodshould be
您应该在子类中调用 NSCoding 方法的超类实现
You should call the superclass implementation of the NSCoding methods in your subclass