macos - 在 Cocoa 中移动文件

标签 macos file cocoa nsfilemanager

我想将文件从一个文件夹移动到另一个文件夹

我用过

[[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/

相关文章:

swift - 如何检测 AppKit 中 TextView 中的换行符?

xcode - iOS 模拟器无法启动

c - C语言将文件读入链表

algorithm - 排序算法 : Big text file with variable-length lines (comma-separated values)

ios - SwiftUI:更改弹出窗口箭头颜色

java - 如果操作系统不属于给定系列(例如 mac),如何激活 pom.xml?

C程序读取大文本文件并将信息存储在struct中

objective-c - 使用 NSTimer 传递原始参数的正确方法

cocoa - 如何检查 self.navigationController 是否为零

ios - 无法拖动或移动自定义 NSWindow