难以理解的initForReadingWithData中的存档/取消存档结果 [英] Archiving / Unarchiving results in initForReadingWithData incomprehensible archive
问题描述
我已经在applicationWillTerminate
上实现了保存,并在applicationWillFinishLoading
上实现了加载.
有一个完整的对象树,都实现了NSCoding
协议,我已经检查了输入的类型.
I've implemented an save on applicationWillTerminate
and load on applicationWillFinishLoading
.
There is a complete object tree, all implement the NSCoding
protocol and I've check the types I enter.
其中一个类还将NSMutableData
存储到NSKeyedArchive
,我怀疑这有时可能会破坏取消存档.奇怪的是,有时它起作用,有时却不起作用.我怀疑NSMutableData
中的某些内容会破坏存档.
One of the classes also stores an NSMutableData
to the NSKeyedArchive
, which I suspect might break unarchiving occasionally. Weirdly enough, sometimes it works and sometimes it doesn't. I suspect some content in the NSMutableData
will break the archiving.
我在所有对象上都使用encodeObject,但布尔型和整数型除外,在这里我使用正确的对应方法(encodeBool:forKey:
和encodeInt:forKey:
)
I use encodeObject on all objects, except for the bools and int where I use the correct corresponding method (encodeBool:forKey:
and encodeInt:forKey:
)
更清楚的是:该代码确实有效,有时它能够重建相当完整的对象图,而并非总是如此.
To be more clear: the code really does work, sometimes is it able to rebuild a rather complete object graph, just not all the time.
我收到的错误消息是:
initForReadingWithData incomprehensible archive 0x62, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x30, 0x30
已添加:代码失败,它的NSMutableData
为10+ MB
Added: code which is failing, it an NSMutableData
of 10+ MB
- (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,它也会失败.似乎不是大小,而是内容问题?
When I remove the self.data encoding and decoding it always seem to work. It also fails with smaller sized self.data. Doesn't seem size but content issue?
当我确实将nsmutabledata写入文件时,试图打开该文件,适当的列表编辑器显示错误:
Tried to open the file when I did write the nsmutabledata to it, the propertly list editor displays the error:
"Conversion of string failed. The string is empty."
plutil也给出此错误:
plutil also gives this error:
"$ plutil -lint nzbvortex.state nzbvortex.state: Conversion of string failed. The string is empty."
推荐答案
似乎通过NSMutableArray存储超过230000字节会导致NSKeyedArchiver创建损坏的plist文件.
It seems that store more than around 230000 bytes via an NSMutableArray will cause the NSKeyedArchiver to create a broken plist file.
220000件作品,250000件作品没有.是否搜索了允许的确切数量.
220000 works, 250000 didn't. Did search for the exact amount that is allowed.
这篇关于难以理解的initForReadingWithData中的存档/取消存档结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!