I suddenly see errors when trying to save documents in my document based app on macOS High Sierra (beta build 17A362a). The error reported in the UI is:
“The document “Example” could not be saved. The file doesn’t exist.”
The file is definitely on disk. My document is backed by an NSFileWrapper and I tracked this down to this line when NSDocument.writeToURL is called:
- (BOOL)writeToURL:(NSURL *)url ofType:(NSString *)typeName forSaveOperation:(NSSaveOperationType)saveOperation originalContentsURL:(NSURL *)absoluteOriginalContentsURL error:(NSError * _Nullable __autoreleasing *)outError
{
return [self.myFileWrapper writeToURL:url options:NSFileWrapperWritingAtomic originalContentsURL:absoluteOriginalContentsURL error:outError];
}
Writing out the NSFileWrapper logs this error to the console:
-[NSFileWrapper fileWrappers] tried to read the file wrapper's contents lazily but an error occurred: The file couldn’t be opened because it doesn’t exist.
My app works fine on Sierra and before.
Furthermore, if I simply traverse the "NSFileWrapper.fileWrappers" property to print out their respective names, the error does not occur! Here's an example:
- (void)_logFileWrapper:(NSFileWrapper*)fileWrapper prefix:(NSString*)prefix{
NSLog(@"%@%@: %@", prefix, fileWrapper.isDirectory ? @"FOLDER":@"File ", fileWrapper.preferredFilename);
if (fileWrapper.isDirectory) {
// NOTE: Just iterating over the fileWrappers makes the issue go away:
for (id child in [fileWrapper.fileWrappers allValues]) {
[self _logFileWrapper:child prefix:[prefix stringByAppendingString:@" "]];
}
}
}
For reference, I filed the bug: 34546920.