【问题标题】:copyItemAtPath:toPath:error: is returning NOcopyItemAtPath:toPath:error: 正在返回 NO
【发布时间】:2012-08-03 16:49:56
【问题描述】:

我正在阅读的章节是在谈论NSFileManager,作者说要创建一个名为testFile的空文件。 testFilemain.m 位于同一文件夹下。我没有创建newfile。我无法复制testFile,它是returning 2NSLog@"couldnt copy file"。我试图把 toPath 的论点: as @"/Users/el/Desktop/prog/prog/newfile"

int main (int argc, char *argv[]) {
    @autoreleasepool {
        NSString *fName = @"/Users/el/Desktop/prog/prog/testFile";
        NSFileManager *fm = [NSFileManager defaultManager];

        if ([fm fileExistsAtPath: fName] == NO) {
            NSLog(@"couldnt find file");
            return 1;
        }

        if ([fm copyItemAtPath:fName toPath:@"newfile" error:NULL] == NO) {
            NSLog(@"couldnt copy file");
            return 2;
        }

【问题讨论】:

    标签: objective-c macos nsurl nsfilemanager file-copying


    【解决方案1】:

    第二条路径也必须是绝对路径。

    [fm copyItemAtPath: @"/Users/el/Desktop/prog/prog/testFile" toPath: @"/Users/el/Desktop/prog/prog/newFile" error: NULL];
    

    注意:您应该使用基于NSURL 的方法,而不是基于NSString 的方法。

    【讨论】:

    • 抱歉,[fm copyItemAtPath:@"/Users/el/Desktop/prog/prog/testFile" toPath:@"/Users/el/Desktop/prog/prog/newfile" error:NULL] 没用,您还有其他建议吗?哦,我只是按照作者的指示进行操作。也许他稍后会去NSURL
    • @Emily 您确定这条路径存在并且您有权在其中写入吗?我不知道为什么它会失败。如果您尝试使用 URL 变体:[fm copyItemAtURL: [NSURL fileURLWithPath: @"/Users/el/Desktop/prog/prog/testFile"] toURL: [NSURL fileURLWithPath: @"/Users/el/Desktop/prog/prog/newFile"] error: nil],会怎样?
    • @Emily 你也可以做的是实际请求错误信息,如下所示:NSError *error = nil; [fm copyItemAtURL: (…) toURL: (…) error: &error]; if (error) NSLog(error);
    • 哦,是的,对不起。原来文件被复制了,但它被隐藏了。 尴尬非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 2011-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多