我想将文件从一个文件夹移动到另一个文件夹
我用过
[[NSFileManager defaultManager]moveItemAtPath:@"Folder/Filename.fileExtension" toPath:@"FolderToMoveTheFile" error:nil];
是否还有其他内容需要输入错误:?
为什么它不移动我的文件?
最佳答案
这样做会出现错误:
NSError* error;
if (![[NSFileManager defaultManager]moveItemAtPath:@"Folder/Filename.fileExtension" toPath:@"FolderToMoveTheFile" error:&error])
{
// handle error, typically using the NSError object pointed to by the error variable
// In an app, you might want to pass it to a -presentError:... method of a convenient responder
// This is good enough for debugging:
NSLog(@"failed to move file: %@", error);
}
toPath:
之后的第二条路径必须是路径包括目标处的文件名。仅指定要将文件移动到的目录的路径是不正确的。
此外,您通常应该使用绝对路径,而不是相对路径。您可以使用相对路径,但这取决于进程的当前工作目录。对于从 Finder 或 Dock 启动的应用程序来说,这是任意的。它实际上只对从 shell(例如终端窗口)启动的命令行工具有意义,用户可能期望 cd
到目录,然后提供相对路径作为命令行参数。
关于macos - 在 Cocoa 中移动文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37773649/