我在缓存目录中移动图像文件时遇到问题。我的要求是当我删除一个文件时,例如 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.)
请帮我 。提前致谢。