0

我在缓存目录中移动图像文件时遇到问题。我的要求是当我删除一个文件时,例如 image3.png,然后在该位置(image4.png, .....,image9.png) 将移动到之前的位置。我想将 image4.png 之类的文件重命名为 image 3.png。下面是我的代码

NSError *error=nil;

NSString       *imgName            =    [NSString stringWithFormat:@"image%d.png",3];
NSFileManager  *fileManager        =    [NSFileManager defaultManager];
NSArray        *paths              =    NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString  *documentsDirectory      =    [paths objectAtIndex:0];
NSString  *fullPath                =    [documentsDirectory stringByAppendingPathComponent:imgName];
[fileManager    removeItemAtPath: fullPath      error:NULL];

for (int i=3; i<=9 ; i++) {

    NSString    *imgName1   =    [NSString stringWithFormat:@"image%d.png",i];
    NSString    *getImagePath1 = [documentsDirectory stringByAppendingPathComponent:imgName1];

    NSString    *imgName2   =    [NSString stringWithFormat:@"image%d.png",i+1];
    NSString    *getImagePath2 = [documentsDirectory stringByAppendingPathComponent:imgName2];

    if ([fileManager moveItemAtPath:getImagePath2 toPath:getImagePath1 error:&error] != YES)
        NSLog(@"Unable to move file: %@", [error localizedDescription]);

    // Show contents of Documents directory
    NSLog(@"Documents directory: %@", 
          [fileManager contentsOfDirectoryAtPath:documentsDirectory error:&error]);

}

但它显示以下错误

 Unable to move file: The operation couldn’t be completed. (Cocoa error 516.)

请帮我 。提前致谢。

4

1 回答 1

0

NSFileManager文档开始moveItemAtPath:toPath:error:

如果 dstPath 中已存在同名项目,则此方法中止移动尝试并返回适当的错误

先删除要覆盖的文件。

于 2012-10-04T07:22:46.127 回答