【问题标题】:Error when copy file from one place to another in NSFileManager在 NSFileManager 中将文件从一个地方复制到另一个地方时出错
【发布时间】:2013-07-12 06:49:11
【问题描述】:

当我使用 NSFileManager 通过 copyItemAtPath 将一个文件(目录)从一个地方复制到另一个地方时,它给了我错误。

代码如下:

 [[NSFileManager defaultManager]copyItemAtPath:@"/Users/name/Documents/localhost/websiteDesign/_Software/" toPath:@"/Volumes/NAME/" error:&handleError];
 NSLog(@"%@", [handleError description]);

这是错误:

 Error Domain=NSCocoaErrorDomain Code=516 "“_Software” couldn’t be copied to “Volumes”   because an item with the same name already exists." UserInfo=0x102842d00
 {NSSourceFilePathErrorKey=/Users/name/Documents/localhost/websiteDesign/_Software/, NSUserStringVariant=(Copy), NSDestinationFilePath=/Volumes/NAME/, NSFilePath=/Users/name/Documents/localhost/websiteDesign/_Software/, NSUnderlyingError=0x10283f8e0 "The operation couldn’t be completed. File exists"}

但是,当我将目标文件的名称 @"/Volumes/NAME/" 更改为 @"/Volumes/aaa/" 之类的其他名称时,错误就会消失。我不知道为什么。谢谢!!

【问题讨论】:

  • 您了解错误信息吗? “因为已经存在同名项目”...

标签: objective-c macos cocoa nsfilemanager


【解决方案1】:

您需要指定完整的路径名,包括实际的文件名。

Apple's documentation for copyItemAtPath:toPath:error: 状态,对于“toPath:”参数:

放置 srcPath 副本的路径。 此路径必须 在新位置包含文件或目录的名称。 这 参数不能为 nil。

你现在正在做的是试图用一个文件覆盖整个硬盘驱动器(卷)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 2021-04-06
    相关资源
    最近更新 更多