2

I am trying to move a file from a path to a directory at another path. I have the code in hand. But, its not working properly. I don't know what is wrong with that code.

if ([[NSFileManager defaultManager] moveItemAtPath:"path to be moved" toPath:"destination path" error:&error]) {
    NSLog(@"Moved");
} else {
    NSLog(@"Not moved:%@", [error localizedDescription]);
}  

Actually, if the moving process is successful, "Moved" will be printed. But the result prints the error message "The operation couldn’t be completed. (Cocoa error 512.)". Please help me to come out of this problem. Thanks in advance.

iOS
  • 3,526
  • 3
  • 37
  • 82
  • Try copyItemAtPath:toPath:error: instead of moveItemAtPath:toPath:error:, – visakh7 May 09 '11 at 10:19
  • Actually I am not moving the file from mainBundle. I have to move a selected file from document directory to a selected folder. – iOS May 09 '11 at 10:56

1 Answers1

2

Use the below function

[[NSFileManager defaultManager] copyItemAtPath:mainBundleFilePath 
                                        toPath:destPath 
                                         error:&err]

For more check the below SO post,

iPhone - copying a file from resources to Documents gives an error

Community
  • 1
  • 1
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76