2

这是我的代码。

NSArray *pathSong = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *toPath = [[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"Songs"];
    NSString *fromPath=[[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"abc"];
    NSString *strdestination = [fromPath stringByAppendingPathComponent:@"sg.mp3"];
    NSError *Error;
    if([[NSFileManager defaultManager]fileExistsAtPath:strdestination]){
        if([[NSFileManager defaultManager]copyItemAtPath:strdestination toPath:toPath error:&Error]==NO){
            UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"copy" message:[NSString stringWithFormat:@"%@",Error] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [Alert show];
        }
        else{
            UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"Not copy" message:[NSString stringWithFormat:@"%@",Error] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [Alert show];
        }
    }

我收到错误徽标:

错误域 = NSCocoaErrorDominCode = 516“操作无法完成。(可可箭头 516。)”用户信息 = 0x681abf0

NSUnderlyingError =0x681b920 "操作无法完成。文件存在"

abc 文件夹中没有歌曲名称“sg.mp3”,但我仍然收到文件存在错误。不知道我哪里做错了?

4

4 回答 4

11

您的代码中有两个问题:

  1. 如果文件已经存在,您需要删除它
  2. 您需要为目标文件指定一个名称,这意味着如果您使用如下:

NSString *toPath = [[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"Songs"];

[[NSFileManager defaultManager]copyItemAtPath:strdestination toPath:toPath error:&Error];

然后如果发生复制,它会将 Sg.mp3 文件复制为无任何类型的歌曲。

所以你需要这样写:

NSArray *pathSong = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *tempPath = [[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"Songs"];
NSString *toPath = [tempPath stringByAppendingPathComponent:@"yourFileName.mp3"];
NSString *fromPath = [[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"abc"];
NSString *strdestination = [fromPath stringByAppendingPathComponent:@"sg.mp3"];
NSError *Error = nil;

if([[NSFileManager defaultManager]fileExistsAtPath:strdestination])
{

  if([[NSFileManager defaultManager]copyItemAtPath:strdestination toPath:toPath error:&Error]==NO)
   {
       UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"copy" message:[NSString stringWithFormat:@"%@",Error] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
       [Alert show];
   }
   else
   {
      [fileManager removeItemAtPath:strdestination error:NULL];
       UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"Not copy" message:[NSString stringWithFormat:@"%@",Error] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
       [Alert show];
   }
}

如果目标文件存在,此代码将删除该文件,然后将文件夹复制到sg.mp3具有名称的文件夹abcSongsyourFileName.mp3

于 2012-08-17T17:31:19.290 回答
3

借鉴 Midhun MP 的答案,这里有一个帮手

BOOL moveFile(NSString *srcPath, NSString *dstPath)
{
    NSLog(@"moving %@ -> %@", srcPath, dstPath);

    NSFileManager *fm = [NSFileManager defaultManager];
    if ([fm fileExistsAtPath:dstPath])
    {
        // in my usecase this is a hard error, bolt to prevent overwriting
        return NO;
    }

    if ([fm fileExistsAtPath:srcPath])
    {
        NSError *error = nil;
        NSString *destDir = [dstPath stringByDeletingLastPathComponent];
        [fm createDirectoryAtPath:destDir withIntermediateDirectories:YES attributes:nil error:nil];

        if ([[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:dstPath error:&error]==NO)
        {
            NSLog(@"failure declassing %@", srcPath);
            return NO;
        }
        else
        {
            [fm removeItemAtPath:srcPath error:NULL]; // gr8t success
            return YES;
        }
    }

    return NO;
 }
于 2015-02-04T12:50:01.283 回答
0

我认为这是因为您试图用您的副本覆盖文件。

检查您的权限掩码,尝试使用缓存而不是文档目录。

你的意思是if(!fileExistsAtPath)

于 2012-08-17T11:48:58.877 回答
0

您需要删除已经存在的文件:

NSArray *pathSong = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *toPath = [[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"Songs"];
NSString *fromPath=[[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"abc"];
NSString *strdestination = [fromPath stringByAppendingPathComponent:@"sg.mp3"];
NSError *Error;


//DELETE THE FILE AT THE LOCATION YOU'RE COPYING TO
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:strdestination error:NULL];


if([[NSFileManager defaultManager]copyItemAtPath:strdestination toPath:toPath error:&Error]==NO){
        UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"copy" message:[NSString stringWithFormat:@"%@",Error] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [Alert show];
    }
else{
        UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"Not copy" message:[NSString stringWithFormat:@"%@",Error] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [Alert show];
    }
于 2012-08-17T11:50:50.050 回答