我有一个TableView控制器,它为每个单元格获取一个标题。标题类是一个符合NSSecureEnocding的NSobject。插入新对象时,我将创建一个新的Title并使用setTitle:forKey将NSData保存到应用程序的Documents文件夹中的子文件夹中。然后让tableView的单元格运行一个方法:getTitle:forKey。
我在控制台中收到一条错误消息:getTitleForkey unarchivingError错误域= NSCocoaErrorDomain代码= 4864“ ***-[NSKeyedUnarchiver _initForReadingFromData:error:throwLegacyExceptions:]:数据为空;您是否忘记将-finishEncoding发送给NSKeyedArchiver ?
特别是关于NSKeyedArchiver,我有点不满意,但是我应该在哪里将-finishEncoding发送到NSKeyedArchiver
我查看了Apple的NSKeyedArchiver(https://developer.apple.com/documentation/foundation/nskeyedarchiver?language=objc)文档,该文档告诉我保存文件后需要调用finishEncoding,但不确定在哪里调用。
非常感谢您的帮助!
这是我的存档代码:
NSArray *dirPaths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *docsDir = dirPaths[0];
NSString *titlesPath = [docsDir stringByAppendingPathComponent:@"/Titles"];
NSString *fileName = [key stringByAppendingString:@".tpt"];
NSString* path = [titlesPath stringByAppendingPathComponent:fileName];
NSData *file;
file = [NSKeyedArchiver
archivedDataWithRootObject:title
requiringSecureCoding:YES
error:nil];
[[NSFileManager defaultManager] createFileAtPath:path
contents:file
attributes:nil];
我的消息是:
unarchivingError错误域= NSCocoaErrorDomain代码= 4864“ ***-[NSKeyedUnarchiver _initForReadingFromData:error:throwLegacyExceptions:]:数据为空;您是否忘记将-finishEncoding发送给NSKeyedArchiver?