【发布时间】:2010-01-08 09:53:47
【问题描述】:
我在applicationWillTerminate 上实现了保存并在applicationWillFinishLoading 上加载。
有一个完整的对象树,都实现了NSCoding协议,我已经检查了我输入的类型。
其中一个类还将NSMutableData 存储到NSKeyedArchive,我怀疑这可能会偶尔破坏取消归档。奇怪的是,有时它有效,有时则无效。我怀疑NSMutableData 中的某些内容会破坏存档。
我在所有对象上使用 encodeObject,除了 bools 和 int 我使用正确的对应方法(encodeBool:forKey: 和 encodeInt:forKey:)
更清楚一点:代码确实有效,有时它能够重建一个相当完整的对象图,但并非总是如此。
我得到的错误信息是:
initForReadingWithData incomprehensible archive 0x62, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x30, 0x30
添加:失败的代码,它是 10+ MB 的 NSMutableData
- (void)encodeWithCoder:(NSCoder*)encoder {
[encoder encodeObject:self.encodedMessage forKey:@"EncodedMessage"]; //NSData
[encoder encodeObject:self.data forKey:@"Data"]; //NSMutableData (10+MB)
[encoder encodeObject:self.header forKey:@"Header"]; //NSString
[encoder encodeObject:self.fileName forKey:@"FileName"]; //NSString
[encoder encodeInt:self.dataStartIndex forKey:@"DataStartIndex"]; //int
[encoder encodeInt:self.dataEndIndex forKey:@"DataEndIndex"]; //int
}
- (id)initWithCoder:(NSCoder*)decoder {
if (self = [super init]) {
self.encodedMessage = [decoder decodeObjectForKey:@"EncodedMessage"]; //NSData
self.data = [decoder decodeObjectForKey:@"Data"]; //NSMutableData
self.header = [decoder decodeObjectForKey:@"Header"]; //NSString
self.fileName = [decoder decodeObjectForKey:@"FileName"]; //NSString
self.dataStartIndex = [decoder decodeIntForKey:@"DataStartIndex"]; //int
self.dataEndIndex = [decoder decodeIntForKey:@"DataEndIndex"]; //int
}
return self;
}
当我删除 self.data 编码和解码时,它似乎总是有效。 它也因较小的 self.data 而失败。似乎不是大小而是内容问题?
当我将 nsmutabledata 写入文件时尝试打开文件,propertly 列表编辑器显示错误:
"Conversion of string failed. The string is empty."
plutil 也给出了这个错误:
"$ plutil -lint nzbvortex.state nzbvortex.state: Conversion of string failed. The string is empty."
【问题讨论】:
-
我也遇到过这个问题。错误信息与您的完全相同。我在
NSFileCoordinator读取块中使用NSKeyedUnarchiver来取消归档由NSKeyedArchiver在另一个线程中归档的数据。崩溃偶尔会发生,我在调试时无法重现它。你有解决办法吗?
标签: objective-c xcode macos archiving nscoding