2013-11-25 25 views
2

我試圖將文件從一個文件夾複製到另一個文件夾中的文件目錄是這樣的:iPhone:錯誤copyItemAtPath

- (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"} 
+2

只是仔細閱讀錯誤,您的答案是在該名稱的錯誤文件中已經存在 –

回答

8

問題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:dataPath error:&error]; 
0

替換此行:

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

有了:

BOOL success = [[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:dataPath error:&error]; 
2

copyItemAtPath:由於該文件已存在於該位置,因此發生錯誤。所以你應該在複製文件之前做到這一點。

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