繁体   English   中英

该操作无法完成。 没有这样的文件或目录:但是文件存在

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

在使用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"}

但是该文件实际上存在于该位置。 该文件存储在应用程序文档文件包中。

代码:(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);
}

使用各种类的任何...toFile方法时,请确保您使用的是真实路径,而不是错误地使用了文件URL。 您可以通过查看路径来区分这些。 如果路径以file://开头,则无法使用。 如果以斜杠开头/是常规文件路径。 您仍然可以使用NSFileManager来检查文件是否存在。

就像在您的代码中一样,而不是在self.fileURL调用path上调用absoluteString

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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