在自定义类的子类上使用 NSCoding

发布于 2024-12-11 21:49:51 字数 1530 浏览 0 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(2

相守太难 2024-12-18 21:49:51

FileinitWithCoder: 方法中

if ( (self = [super init]) )

应该是

if ( (self = [super initWithCoder:coder]) )

Inside the File's initWithCoder: method

if ( (self = [super init]) )

should be

if ( (self = [super initWithCoder:coder]) )
倾城花音 2024-12-18 21:49:51

您应该在子类中调用 NSCoding 方法的超类实现

- (id)initWithCoder:(NSCoder *)coder {
    if ( (self = [super initWithCoder:coder]) )
    {
        self.fileSizeString = [coder decodeObjectForKey:kFileSizeStringKey];
    }
    return self;
}

You should call the superclass implementation of the NSCoding methods in your subclass

- (id)initWithCoder:(NSCoder *)coder {
    if ( (self = [super initWithCoder:coder]) )
    {
        self.fileSizeString = [coder decodeObjectForKey:kFileSizeStringKey];
    }
    return self;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文