存档/取消存档导致 initForReadingWithData 无法理解存档

Ger*_*nis 5 macos xcode objective-c archiving nscoding

我已经实现了保存applicationWillTerminate和加载applicationWillFinishLoading。有一个完整的对象树,所有对象树都实现了NSCoding协议,并且我已经检查了我输入的类型。

其中一个类还存储NSMutableDataNSKeyedArchive,我怀疑这有时可能会破坏解档。奇怪的是,有时有效,有时无效。我怀疑其中的某些内容NSMutableData会破坏存档。

我对所有对象使用encodeObject,除了 bools 和 int,我在其中使用正确的相应方法 (encodeBool:forKey:encodeInt:forKey:)

更清楚地说:代码确实有效,有时它能够重建一个相当完整的对象图,但并非总是如此。

我收到的错误消息是:

initForReadingWithData incomprehensible archive 0x62, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x30, 0x30
Run Code Online (Sandbox Code Playgroud)

添加:失败的代码,大小超过NSMutableData10 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;
}
Run Code Online (Sandbox Code Playgroud)

当我删除 self.data 编码和解码时,它似乎总是有效。如果 self.data 较小,它也会失败。看起来不是大小问题而是内容问题?

当我将 nsmutabledata 写入文件时尝试打开该文件,正确的列表编辑器显示错误:

"Conversion of string failed. The string is empty."
Run Code Online (Sandbox Code Playgroud)

plutil 也给出了这个错误:

"$ plutil -lint nzbvortex.state nzbvortex.state: Conversion of string failed. The string is empty."
Run Code Online (Sandbox Code Playgroud)

hat*_*nch 5

FWIW 我也遇到过这个问题,这就是我发现的。

报告的字节 0x62、0x70、0x6c 等是二进制属性列表开头的魔术字符串“bplist”的一部分,NSKeyedArchiver 默认使用该列表。

二进制属性列表将元数据存储在预告片中(即在数据的末尾)。因此,如果它被截断,整个 plist 将变得不可读。

如果您想检查您是否遇到了这种情况,可以使用 Cocotron ( http://code.google.com/p/cocotron/source/browse/Foundation/NSPropertyList/ ) 中的 NSPropertyListReader_binary1 来查看文件格式的工作原理。

希望这对某人有帮助!


Ger*_*nis 2

看来通过 NSMutableArray 存储超过大约 230000 字节将导致 NSKeyedArchiver 创建损坏的 plist 文件。

220000 个有效,250000 个无效。是否搜索了允许的确切金额。