【问题标题】:Error when trying to copy file尝试复制文件时出错
【发布时间】:2013-06-04 00:45:01
【问题描述】:

我正在尝试使用 NSFileManager 将临时文件复制到另一个位置。但是它失败并抱怨其中一个文件不存在。

 //Copy temp file
    NSError *error;
    BOOL exists = [fileManager fileExistsAtPath:chapterFileTemp];
    exists = [fileManager fileExistsAtPath:chapterFile];

    [fileManager copyItemAtURL:[NSURL fileURLWithPath:chapterFileTemp]
                         toURL:[NSURL fileURLWithPath:chapterFile]
                         error:&error];

    //Delete temp path
    [fileManager removeItemAtURL:[NSURL fileURLWithPath:chapterFileTemp] error:&error];

我在复制操作时遇到错误

(Error Domain=NSCocoaErrorDomain Code=260 "操作无法完成。(Cocoa 错误 260.)" UserInfo=0x1c5190b0 {NSFilePath=/var/mobile/Applications/57727CCD-88AD-4D84-8C78-EA8100645C9B/ Documents/119/myFileTemp.temp, NSUnderlyingError=0x1c527960 “操作无法完成。没有这样的文件或目录”)。

现在第一个 BOOL 返回 YES,第二个返回 NO。这是预期的。

失败的原因是什么

谢谢。

【问题讨论】:

  • 注意:为什么要创建 NSURL?只需使用 copyItemAtPath:toPath:error.
  • 是的,最初我的代码是这样的……但同样的错误。

标签: ios ios6 afnetworking nsfilemanager


【解决方案1】:

当目标路径缺少目录时,这是可能的。 确保您要复制源文件的位置包含所有必需的目录。

【讨论】:

  • 对,目录不见了。非常感谢。
【解决方案2】:

如果正如 +Apurv 所说,目标目录不存在,那么你应该做两件事:

  1. 确保您的目标位置chapterFile 是在您有权访问的位置创建的。 iOS 应用程序在可以写入文件的位置受到限制;您需要创建一个符合 iOS App 限制的路径。见URLsForDirectory:inDomains
  2. 调用createDirectoryAtPath:withIntermediateDirectories:attributes:error: 创建目标目录。

在这两件事之后,copyItem 方法应该可以工作了。

使用,来自NSFileManager

- (BOOL)createDirectoryAtPath:(NSString *)path
  withIntermediateDirectories:(BOOL)createIntermediates
                   attributes:(NSDictionary *)attributes
                        error:(NSError **)error

createIntermediatesYES

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-05
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 2014-10-22
    • 1970-01-01
    相关资源
    最近更新 更多