Вы бы использовали moveItemAtPath:toPath:error:
метод NSFileManager
, например, так:
NSString *jpgPathOne = [NSHomeDirectory() stringByAppendingPathComponent:[@"Documents/" stringByAppendingString:@"Photo1.jpg"]];
NSString *jpgPathTwo = [NSHomeDirectory() stringByAppendingPathComponent:[@"Documents/" stringByAppendingString:@"Photo2.jpg"]];
NSString *jpgPathThree = [NSHomeDirectory() stringByAppendingPathComponent:[@"Documents/" stringByAppendingString:@"Photo3.jpg"]];
NSFileManager *localFileManager = [[NSFileManager alloc] init];
// ... delete Photo2
NSError *deleteError = nil;
BOOL deleted = [localFileManager removeItemAtPath:jpgPathTwo error:&deleteError];
if (!deleted || deleteError) {
NSLog(@"ERROR Deleting file: %@\n%@ - %@", jpgPathTwo, [deleteError localizedDescription], [deleteError localizedFailureReason]);
} else {
// ... If delete worked, rename Photo3 to Photo2...
NSError *renameError = nil;
BOOL renamed = [localFileManager moveItemAtPath:jpgPathThree toPath:jpgPathTwo error:&renameError];
if (!renamed || renameError) {
NSLog(@"ERROR Moving file: %@ to %@!\n%@ - %@", jpgPathThree, jpgPathTwo, [renameError localizedDescription], [renameError localizedFailureReason]);
}
}
[localFileManager release];
Это не проверено, но оно должно работать:
- (BOOL)deleteAndRename:(NSString *)filePath {
BOOL success = NO;
NSError *error = nil;
NSFileManager *fileManager = [[NSFileManager alloc] init];
if ([fileManager fileExistsAtPath:filePath]) {
success = [fileManager removeItemAtPath:filePath error:&error];
if (success) {
error = nil;
NSString *prevFilePath = filePath;
NSString *photoNumber = [[filePath stringByDeletingPathExtension] stringByReplacingOccurrencesOfString:@"Photo" withString:@""];
NSString *nextSequentialFile = [filePath stringByReplacingOccurrencesOfString:photoNumber withString:[NSString stringWithFormat:@"%d", ([photoNumber intValue] + 1)] options:NSBackwardsSearch range:NSRangeFromString(filePath)];
BOOL moveSuccess = NO;
while ([fileManager fileExistsAtPath:nextSequentialFile]) {
moveSuccess = [fileManager moveItemAtPath:nextSequentialFile toPath:prevFilePath error:&error];
if (moveSuccess) {
prevFilePath = nextSequentialFile;
photoNumber = [[prevFilePath stringByDeletingPathExtension] stringByReplacingOccurrencesOfString:@"Photo" withString:@""];
nextSequentialFile = [prevFilePath stringByReplacingOccurrencesOfString:photoNumber withString:[NSString stringWithFormat:@"%d", ([photoNumber intValue] + 1)] options:NSBackwardsSearch range:NSRangeFromString(prevFilePath)];
} else {
NSLog(@"*** Error Moving File: %@ -> %@ ***", nextSequentialFile, filePath);
if (error) {
NSLog(@"%@ - %@", [error localizedDescription], [error localizedFailureReason]);
}
success = NO;
break;
}
}
success = moveSuccess;
} else {
NSLog(@"*** Error Deleting File: %@ ***", filePath);
if (error) {
NSLog(@"%@ - %@", [error localizedDescription], [error localizedFailureReason]);
}
}
} else {
NSLog(@"*** No such file: %@ ***", filePath);
success = NO;
}
return success;
}