我在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写入文件时,尝试打开该文件,正确的列表编辑器显示错误:
"Conversion of string failed. The string is empty."plutil也会给出这个错误:
"$ plutil -lint nzbvortex.state nzbvortex.state: Conversion of string failed. The string is empty."发布于 2010-01-09 05:24:31
通过NSMutableArray存储超过230000字节似乎会导致NSKeyedArchiver创建一个损坏的plist文件。
220000有效,250000无效。确实搜索了允许的确切数量。
发布于 2013-04-10 22:19:10
FWIW我也遇到过这个问题,这是我发现的。
报告的0x62、0x70、0x6c等字节是位于二进制属性列表开头的魔术字符串"bplist“的一部分,NSKeyedArchiver默认使用该字符串。
二进制属性列表将元数据存储在尾部(即,在数据的末尾)。因此,如果它被截断,整个plist将变得不可读。
如果您想检查是否发生了这种情况,您可以使用Cocotron (http://code.google.com/p/cocotron/source/browse/Foundation/NSPropertyList/)的NSPropertyListReader_binary1来查看文件格式是如何工作的。
希望这对某些人有帮助!
发布于 2010-01-08 18:10:15
对于bool和int,有两个方法:encodeBool:forKey:和encodeInt:forKey: (取自NSKeyedArchiver reference)。
对于NSMutableData,您应该使用encodeObjectForKey:对其进行归档,并使用decodeObjectForKey:对其进行解压。
您可以参考此useful guide了解更多案例。
https://stackoverflow.com/questions/2026720
复制相似问题