[英]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:options:error:
保存文件时遇到一个奇怪的问题writeToFile:options:error:
在第一种情况下,我遇到以下问题:
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) 代码:(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. 使用各种类的任何
...toFile
方法时,请确保您使用的是真实路径,而不是错误地使用了文件URL。 You can tell these apart by looking at the path. 您可以通过查看路径来区分这些。 If the path starts with
file://
it can't be used. 如果路径以
file://
开头,则无法使用。 If it starts with a slash /
it is a regular file path. 如果以斜杠开头
/
是常规文件路径。 Still you might use NSFileManager
to check if the file exists. 您仍然可以使用
NSFileManager
来检查文件是否存在。
As in your code instead of calling absoluteString
on self.fileURL
call path
. 就像在您的代码中一样,而不是在
self.fileURL
调用path
上调用absoluteString
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.