Error The operation couldn’t be completed. (Cocoa error 516.)

This is very common error that happens when you try to create a file on the file system and a file with the same name already exists. You can avoid that by checking whether a file with the name already exists or not. If it does exists then remove it and try to create again.

Error 516 is NSFileWriteFileExistsError - You can't move a file to a place where a file already exists.

The below code does not check for this and can lead to above error:

            NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            NSString *destSpecFile = [NSString stringWithFormat:@"%@/%@", docDir,gallerySpecFile];
           
            NSError *err;
                   
            NSString *srcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:gallerySpecFile];
            CCLOG(@"Src %@",srcPath);
            CCLOG(@"Descr %@",destSpecFile);
           
            [[NSFileManager defaultManager] copyItemAtPath:srcPath
                                                    toPath:destSpecFile
                                                     error:&err];

Before attempting the copyItemAtPath, check the destination file and if there, remove it.

你可能感兴趣的:(Error The operation couldn’t be completed. (Cocoa error 516.))