Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios iPhone:copyItemAtPath中出错_Ios_Objective C_Nsfilemanager - Fatal编程技术网

Ios iPhone:copyItemAtPath中出错

Ios iPhone:copyItemAtPath中出错,ios,objective-c,nsfilemanager,Ios,Objective C,Nsfilemanager,我正在尝试将文件从一个文件夹复制到文档目录中的另一个文件夹,如下所示: - (void)saveFile:(NSString *)folderPath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; N

我正在尝试将文件从一个文件夹复制到文档目录中的另一个文件夹,如下所示:

    - (void)saveFile:(NSString *)folderPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"destination.png"];

    NSError *error = nil;
    NSString *srcPath = [folderPath stringByAppendingPathComponent:@"filename.png"];


    BOOL success = [[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:documentsDirectory error:&error];
    if (error)
    {
        NSLog(@"%@",error);
    }

    if (success == YES)
    {
        NSLog(@"Copied");
    }
    else
    {
        NSLog(@"Not Copied");
    }
}
当我记录错误时,它会给我以下消息:

Error Domain=NSCocoaErrorDomain Code=516 "The operation couldn’t be completed. (Cocoa error 516.)" UserInfo=0x9d665a0 {NSUserStringVariant=(
    Move
), NSFilePath=/Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/BC37C9D7-7995-47C1-8131-2B07BADCBECB/Documents/foldername/B713320C-2CA0-4FD3-93F6-71D76B102B83/src.png, NSDestinationFilePath=/Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/BC37C9D7-7995-47C1-8131-2B07BADCBECB/Documents, NSUnderlyingError=0x9d41c60 "The operation couldn’t be completed. File exists"}
第1期: 发生此问题的原因是,目录中已包含具有相同文件名的文件

您应该检查文件夹中是否存在该文件,如下所示:

- (void)saveFile:(NSString *)folderPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"destination.png"];

    NSError *error = nil;
    NSString *srcPath = [folderPath stringByAppendingPathComponent:@"filename.png"];

    if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    {
        //removing file
        if (![[NSFileManager defaultManager] removeItemAtPath:dataPath error:&error])
        {
            NSLog(@"Could not remove old files. Error:%@",error);
        }
    }
    BOOL success = [[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:dataPath error:&error];
    if (success == YES)
    {
        NSLog(@"Copied");
    }
    else
    {
        NSLog(@"Not Copied %@", error);
    }
}
问题2: 必须提供文件名而不是目录

替换:

BOOL success = [[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:documentsDirectory error:&error];
BOOL success = [[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:documentsDirectory error:&error];
与:

替换此行:

与:


copyItemAtPath
:给出错误,因为该文件已存在于该位置。所以您应该在复制文件之前执行此操作

if ([[NSFileManager defaultManager] fileExistsAtPath: srcPath])
        [[NSFileManager defaultManager] removeItemAtPath: srcPath error:nil];

只要仔细阅读错误,您的答案就在错误文件中,并且该名称已经存在
BOOL success = [[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:dataPath error:&error];
if ([[NSFileManager defaultManager] fileExistsAtPath: srcPath])
        [[NSFileManager defaultManager] removeItemAtPath: srcPath error:nil];