NSKeyedArchiver 返回意外的类?

发布于 2024-12-10 07:03:01 字数 1096 浏览 3 评论 0原文

我有一个扩展 NSString 的自定义类。我正在尝试使用 NSKeyedArchiver 对其进行序列化(用于拖/放)。该类覆盖 ...Coder 方法:

- (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        data = [[aDecoder decodeObjectForKey:@"data"] copy];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [super encodeWithCoder:aCoder];
    [aCoder encodeObject:self.data forKey:@"data"];
}

但是当我尝试实际运行归档/取消归档时:

MyClass *object = [[MyClass alloc] init];
[pboard setData:[NSKeyedArchiver archivedDataWithRootObject:object] forType:SACP_WRAPPER_DRAG_TYPE];
NSLog(@"Wrote data for class %@", [object class]);

...

id item = [NSKeyedUnarchiver unarchiveObjectWithData:[[info draggingPasteboard] dataForType:SACP_WRAPPER_DRAG_TYPE]];
NSLog(@"Read data for class %@", [item class]);

输出不是我所期望的:< /强>

2011-10-15 18:56:22.898 MyApp[7402:707] Wrote data for class ASCIIString
2011-10-15 18:56:23.345 MyApp[7402:707] Read data for class __NSCFString

I have a custom class that extends NSString. I'm attempting to serialize it (for drag/drop) using an NSKeyedArchiver. The class overrides the ...Coder methods:

- (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        data = [[aDecoder decodeObjectForKey:@"data"] copy];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [super encodeWithCoder:aCoder];
    [aCoder encodeObject:self.data forKey:@"data"];
}

But when I try to actually run through the archiving/unarchiving:

MyClass *object = [[MyClass alloc] init];
[pboard setData:[NSKeyedArchiver archivedDataWithRootObject:object] forType:SACP_WRAPPER_DRAG_TYPE];
NSLog(@"Wrote data for class %@", [object class]);

...

id item = [NSKeyedUnarchiver unarchiveObjectWithData:[[info draggingPasteboard] dataForType:SACP_WRAPPER_DRAG_TYPE]];
NSLog(@"Read data for class %@", [item class]);

The output is not what I am expecting:

2011-10-15 18:56:22.898 MyApp[7402:707] Wrote data for class ASCIIString
2011-10-15 18:56:23.345 MyApp[7402:707] Read data for class __NSCFString

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

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

发布评论

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

评论(1

冬天的雪花 2024-12-17 07:03:01

NSString 是一个类集群

“NSString 是一个类簇,以及其他 Foundation 类型,例如 NSNumber 和 NSArray:

类簇是 Foundation 框架广泛使用的一种设计模式。类簇将许多私有的、具体的子类分组在一个公共的、抽象的下。 而不会降低其功能丰富性。类集群基于“Cocoa 设计模式”中讨论的抽象工厂设计模式。”

以这种方式对类进行分组简化了面向对象框架的公开可见架构, NSString 文档..您必须为您的子类实现自定义存储机制。我的猜测是你没有这样做,或者如果你这样做了,当你在 super 上调用 NSCoding 方法时,你仍然会看到那些私有类弹出,因为 super将使用 NSString 引用的特定私有类的方法(这取决于其内容)。

NSString is a class cluster:

"NSString is a class cluster, along with other Foundation types such as NSNumber and NSArray:

Class clusters are a design pattern that the Foundation framework makes extensive use of. Class clusters group a number of private, concrete subclasses under a public, abstract superclass. The grouping of classes in this way simplifies the publicly visible architecture of an object-oriented framework without reducing its functional richness. Class clusters are based on the Abstract Factory design pattern discussed in “Cocoa Design Patterns.”"

Make sure you read the 'Subclassing Notes' in the NSString doc... you have to implement a custom storage mechanism for your subclass. My guess is that you aren't doing this, or if you are, you are still seeing those private classes pop up when you call the NSCoding methods on super, because super will use the methods of the specific private class that the NSString refers to (which depends on its contents).

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