简体   繁体   English

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

[英]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.

相关问题 XCode 4 存档/IPA 错误:“操作无法完成。 没有这样的文件或目录” - XCode 4 Archive/IPA Error: “The operation couldn’t be completed. No such file or directory” NSInputStream:read:maxLength:在TCP套接字流上返回错误“操作无法完成。 错误的文件描述符” - NSInputStream:read:maxLength: on a TCP socket stream returns the error “The operation couldn’t be completed. Bad file descriptor” 该操作无法完成。 可可错误4 - The operation couldn’t be completed. Cocoa Error 4 如何解决运行时错误'无法创建带有消息的可写数据库文件'无法完成操作。 (可可错误260.)'。'? - how to solve run time error 'Failed to create writable database file with message 'The operation couldn’t be completed. (Cocoa error 260.)'.'? writeToFile给NSUnderlyingError = 0x6a3d450“该操作无法完成。 是目录” - writeToFile gives NSUnderlyingError=0x6a3d450 “The operation couldn’t be completed. Is a directory” MidiClientCreate“操作无法完成。 (OSStatus错误-50。)” - MidiClientCreate “The operation couldn't be completed. (OSStatus error -50.)” 该操作无法完成。 (可可错误:3840。) - The Operation couldn't be completed. (Cocoa error: 3840.) “这项行动无法完成。 (可可错误512.)“ - “The operation couldn’t be completed. (Cocoa error 512.)” ITLibrary:无法完成操作。 (OSStatus 错误 7011。) - ITLibrary: The operation couldn’t be completed. (OSStatus error 7011.) 操作无法完成。 对等方重置连接 - The operation couldn’t be completed. Connection reset by peer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM