NSKeyedArchiver返回意外类? [英] NSKeyedArchiver returning unexpected class?

查看:129
本文介绍了NSKeyedArchiver返回意外类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

   - )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(@为类%@编写数据,[object class]);

...

id item = [NSKeyedUnarchiver unarchiveObjectWithData:[[info draggingPasteboard] dataForType:SACP_WRAPPER_DRAG_TYPE]];
NSLog(@读取类%@,[item class]的数据);

输出不是我预期的

  2011-10-15 18:56:22.898 MyApp [7402:707]写ASCIIString类的数据
2011-10-15 18 :56:23.345 MyApp [7402:707]读取类__NSCFString的数据


解决方案

NSString是一个类集群: / p>

NSString是一个类集群,以及其他Foundation类型,如NSNumber和NSArray:



类集群基类框架广泛使用的设计模式类集群在一个公共的,抽象的超类下集合了一些私有的,具体的子类这种类的分组简化了一个面向对象框架的公开可见的结构,而不减少它的类集群基于Cocoa设计模式中讨论的抽象工厂设计模式。



确保您阅读了 NSString doc ...您必须实施您的子类的自定义存储机制。我的猜测是,你不是这样做,或者如果你是,你仍然看到这些私人类弹出,当你调用NSCoding方法在 super ,因为 super 将使用NSString引用的特定 private class 的方法(取决于其内容)。


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

解决方案

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).

这篇关于NSKeyedArchiver返回意外类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆