我有一个基于NSDocument的应用程序,使用文件包装器来保存和加载其数据。文档可以具有各种资源,因此我不想将所有内容加载到内存中。也许我做错了某些事情,但只要我更改了一个(内部)文件,然后保存,我就无法读取任何未加载到内存中的文件。
我已将相关代码分离到一个单独的项目中以重现此行为,并获得相同的结果。基本流程如下:
底部的两种方法仅用于更新UI,以便我能看到发生了什么。
- 要更改文件的内容,我会删除现有的fileWrapper并添加新的fileWrapper。这是我找到的唯一更改文件内容的方法,也是我在其他SO答案中看到的方法。 - 当从磁盘加载文档时,我保留fileWrapper,以便我可以使用它(在上面的代码中称为myWrapper)。
苹果文档表示,NSFileWrapper支持惰性加载和增量保存,因此我假定我的代码存在某些根本性缺陷,但我无法看到。
我已将相关代码分离到一个单独的项目中以重现此行为,并获得相同的结果。基本流程如下:
- I load an existing document from disk. The main fileWrapper is a directory filewrapper (I'll call that main) containing two other filewrappers (sub1 and sub2). The two inner filewrappers are not loaded at this point.
- When the user wants to edit sub1, it is loaded from disk.
- The user saves the document
If the user wants to edit the other file (sub2), it cannot load. The error that appears:
-[NSFileWrapper regularFileContents] tried to read the file wrapper's contents lazily but an error occurred: The file couldn’t be opened because it doesn’t exist.
这是我项目中相关的代码:
您可以在此Gist中更轻松地阅读此代码: https://gist.github.com/bob-codingdutchmen/6869871
#define FileName01 @"testfile1.txt"
#define FileName02 @"testfile2.txt"
/**
* Only called when initializing a NEW document
*/
-(id)initWithType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {
self = [self init];
if (self) {
self.myWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil];
NSLog(@"Initializing new document...");
NSString *testString1 = @"Lorem ipsum first sub file";
NSString *testString2 = @"This is the second sub file with completely unrelated contents";
NSFileWrapper *w1 = [[NSFileWrapper alloc] initRegularFileWithContents:[testString1 dataUsingEncoding:NSUTF8StringEncoding]];
NSFileWrapper *w2 = [[NSFileWrapper alloc] initRegularFileWithContents:[testString2 dataUsingEncoding:NSUTF8StringEncoding]];
w1.preferredFilename = FileName01;
w2.preferredFilename = FileName02;
[self.myWrapper addFileWrapper:w1];
[self.myWrapper addFileWrapper:w2];
}
return self;
}
-(NSFileWrapper *)fileWrapperOfType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {
// This obviously wouldn't happen here normally, but it illustrates
// how the contents of the first file would be replaced
NSFileWrapper *w1 = [self.myWrapper.fileWrappers objectForKey:FileName01];
[self.myWrapper removeFileWrapper:w1];
NSFileWrapper *new1 = [[NSFileWrapper alloc] initRegularFileWithContents:[@"New file contents" dataUsingEncoding:NSUTF8StringEncoding]];
new1.preferredFilename = FileName01;
[self.myWrapper addFileWrapper:new1];
return self.myWrapper;
}
-(BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper ofType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {
self.myWrapper = fileWrapper;
return YES;
}
- (IBAction)button1Pressed:(id)sender {
// Read from file1 and show result in field1
NSFileWrapper *w1 = [[self.myWrapper fileWrappers] objectForKey:FileName01];
NSString *string1 = [[NSString alloc] initWithData:w1.regularFileContents encoding:NSUTF8StringEncoding];
[self.field1 setStringValue:string1];
}
- (IBAction)button2Pressed:(id)sender {
// Read from file2 and show result in field2
NSFileWrapper *w2 = [[self.myWrapper fileWrappers] objectForKey:FileName02];
NSString *string2 = [[NSString alloc] initWithData:w2.regularFileContents encoding:NSUTF8StringEncoding];
[self.field2 setStringValue:string2];
}
底部的两种方法仅用于更新UI,以便我能看到发生了什么。
- 要更改文件的内容,我会删除现有的fileWrapper并添加新的fileWrapper。这是我找到的唯一更改文件内容的方法,也是我在其他SO答案中看到的方法。 - 当从磁盘加载文档时,我保留fileWrapper,以便我可以使用它(在上面的代码中称为myWrapper)。
苹果文档表示,NSFileWrapper支持惰性加载和增量保存,因此我假定我的代码存在某些根本性缺陷,但我无法看到。