The code is very dirty; try the following:
- (BOOL)renameFileFrom:(NSString*)oldName to:(NSString *)newName { NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *oldPath = [documentDir stringByAppendingPathComponent:oldName]; NSString *newPath = [documentDir stringByAppendingPathComponent:newName]; NSFileManager *fileMan = [NSFileManager defaultManager]; NSError *error = nil; if (![fileMan moveItemAtPath:oldPath toPath:newPath error:&error]) { NSLog(@"Failed to move '%@' to '%@': %@", oldPath, newPath, [error localizedDescription]); return NO; } return YES; }
and name it using:
if (![self renameFileFrom:@"oldName.pdf" to:@"newName.pdf]) { // Something went wrong }
Better yet, put the renameFileFrom:to: method in the utility class and make it a class method so that it can be called from anywhere in your project.
source share