1

我试图移动文件。下面我正在测试路径是否存在。他们可以,但是 copyItemAtPath 和 moveItemAtPath 似乎都不起作用。

NSString *testUrl = @"/Users/justinshulman/Documents/test2";
if ([[NSFileManager defaultManager]fileExistsAtPath:testUrl]) {
    NSLog(@"yes");
}
NSString *testUrl2 = @"/Users/justinshulman/Documents/test1";
if ([[NSFileManager defaultManager]fileExistsAtPath:testUrl2]) {
    NSLog(@"yes");
}
NSLog(@"%@",testUrl);
NSLog(@"%@",testUrl2);
[[NSFileManager defaultManager]copyItemAtPath:testUrl2 toPath:testUrl error:nil];
[[NSFileManager defaultManager]moveItemAtPath:testUrl2 toPath:testUrl error:nil];
4

4 回答 4

5

这正是您的问题,如果目标文件已经存在,则移动和复制实际上都不会覆盖它。您必须先将其删除,然后将另一个文件复制(或移动)到该 URL。

尝试

[[NSFileManager defaultManager] removeItemAtPath:testUrl error:nil];
[[NSFileManager defaultManager]copyItemAtPath:testUrl2 toPath:testUrl error:nil];

它应该可以正常工作。

于 2013-10-08T10:12:49.403 回答
1

您应该在错误字段中传递NSError对象。

[[NSFileManager defaultManager]copyItemAtPath:testUrl2 toPath:testUrl error:&error];

Error Domain=NSPOSIXErrorDomain Code=17 UserInfo=0x100457e80 "The operation couldn’t be completed.

[[NSFileManager defaultManager]moveItemAtPath:testUrl2 toPath:testUrl error:&error];

Error Domain=NSCocoaErrorDomain Code=512 UserInfo=0x1004a2270

利用replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error:

以确保不会发生数据丢失的方式将第一个 URL 指定的内容替换为第二个 URL 的内容。

于 2013-10-08T10:48:43.153 回答
1

您还应该检查错误而不是传递 nil。

NSError* error = nil;

[[NSFileManager defaultManager]copyItemAtPath:testUrl2 toPath:testUrl error:&error];

if (error != nil) {
   NSLog(@"%@", [error localizedDescription]);
}

它还返回一个关于复制是否成功的布尔值。

添加到@micantox 答案,请始终阅读课程参考。请参阅NSFileManager 的类参考

如果 dstPath 中已存在同名文件,则此方法中止复制尝试并返回适当的错误。

于 2013-10-08T10:18:23.943 回答
0

@justin, First thing it never works only. Because you are trying to copy the source path to destination path where both path are same. Second thing, how NSFileManager copy or move api works is, you have to copy or move source path to different destination path with appending your appropriate path component. For example see the code below:--

NSString *testUrl = @"/Users/home/Documents/source.rtf";
        //
        if ([[NSFileManager defaultManager]fileExistsAtPath:testUrl]) {
            NSLog(@"yes");
        }
//Below destination is folder name which should be exist on your machine or else you can create programmatically as well 
        NSString *testUrl2 = @"/Users/home/Documents/destination";

        NSLog(@"%@",testUrl);
        NSLog(@"%@",testUrl2);
        NSError *err=nil;

    //Now we are copying the souce path to destination folder with appending file name (it can be any your name becuase file manager copy source file contents to your destination file contents)
    //Here given file name is a destination.rtf where you can give any your name. Also this is for copying source contents to destination contents

        NSFileManager *fm=[NSFileManager defaultManager];
        if ([fm copyItemAtPath:testUrl toPath:[testUrl2 stringByAppendingPathComponent:@"destination.rtf"] error:&err])
        {
            NSLog(@"success");
        }
        else
        {
            NSLog(@"%@",[err localizedDescription]);
        }
于 2013-10-08T13:25:04.240 回答