简体   繁体   中英

The operation couldn’t be completed. No such file or directory: but the file exists

I get a weird issue while saving a file with writeToFile:options:error: In the first case I get the following issue:

writeToFile failed with error Error Domain=NSCocoaErrorDomain Code=4 "The file “preferences.plist” doesn’t exist." UserInfo=0xa12c30 {NSFilePath=file:/Users/patrick/Desktop/Untitled.fef/preferences.plist, NSUnderlyingError=0xa0d130 "The operation couldn’t be completed. No such file or directory"}

But the file actually exist in that location. The file is stored in the app document file package.

CODE: (Subclass of NSDocument)

NSString *prefFile = [[[self fileURL] absoluteString] stringByAppendingPathComponent:@"preferences.plist"];

NSError *error;
BOOL succes = [[NSKeyedArchiver archivedDataWithRootObject:documentPreferences] writeToFile:prefFile options:0 error:&error];
if (!succes) {
    NSLog(@"writeToFile failed with error %@", error);
}

Make sure when using any of the ...toFile methods of various classes that you use a real path and not by mistake a file URL. You can tell these apart by looking at the path. If the path starts with file:// it can't be used. If it starts with a slash / it is a regular file path. Still you might use NSFileManager to check if the file exists.

As in your code instead of calling absoluteString on self.fileURL call path .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM