Question

Below is the code I have written to change the name of any file stored in an iOS device "Documents" directory.

When renaming the file using this code, the prefix resolves to...

"file:/localhost/private/var/mobile/Applications/.../Documents/"

instead of the correctly formatted...

"file://localhost/private/var/mobile/Applications/.../Documents/" 

I am losing a slash, and so the rename method fails!!!

I have rewritten this for both NSString and NSURL formats, and both methods present the identical error. I have tested this using the iOS Simulator and on device, and both tests present the identical error.

I suspect I am overlooking something simple, but cannot figure what it is - any help please?

- (void)alterFileNameOrExtension {
    NSError *errorMove = nil;
    NSError __autoreleasing *error = nil;

//note below - entity attribute document.dStringURL has a data type NSString

    NSString *file = [document.dStringURL lastPathComponent];
    NSString *fileName = [file stringByDeletingPathExtension];
    NSString *fileExtension = [file pathExtension];
    NSString *fileNameNew = nil;

//note below - self.tempFileComponent, self.tempFileName & self.tempFileExtension
//note below - are set in the (IBAction) method textFieldDidEndEditing: 
//note below - self.tempFileComponent determines whether the user is attempting to change the file name or the file extension

    if (self.tempFileComponent == 1) {
        fileNameNew = [[self.tempFileName stringByAppendingPathExtension:fileExtension] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    } else if (self.tempFileComponent == 2) {
        fileNameNew = [fileName stringByAppendingPathExtension:self.tempFileExtension];
    } else {
        NSLog(@"%@ - %@ - attempt to determine tempFileComponent has (possible undefined) E~R~R~O~R: %@_", self.className, NSStringFromSelector(_cmd), error.localizedDescription);
    }

    NSString *filePath = [document.dStringURL stringByDeletingLastPathComponent];
    NSString *filePathNew = [filePath stringByAppendingPathComponent:fileNameNew];

    BOOL move = [[NSFileManager defaultManager] moveItemAtPath:document.dStringURL toPath:filePathNew error:&errorMove];
    if (!move) {
        //handle error
    } else {
        //complete process
    }
}

#

With assistance from @Mario, I have adjusted my code and include the working version below for the benefit of others...

#

NSError *errorMove = nil;
NSError __autoreleasing *error = nil;

NSString *file = [document.dStringURL lastPathComponent];
NSString *fileName = [file stringByDeletingPathExtension];
NSString *fileExtension = [file pathExtension];
NSString *fileNameNew = nil;

if (self.tempFileComponent == 1) {
    fileNameNew = [self.tempFileName stringByAppendingPathExtension:fileExtension];
} else if (self.tempFileComponent == 2) {
    fileNameNew = [fileName stringByAppendingPathExtension:self.tempFileExtension];
} else {
    NSLog(@"%@ - %@ - attempt to determine tempFileComponent has (possible undefined) E~R~R~O~R: %@_", self.className, NSStringFromSelector(_cmd), error.localizedDescription);
}

NSURL *fileURLOld = [NSURL URLWithString:document.dStringURL];
NSURL *fileURLPrefix = [fileURLOld URLByDeletingLastPathComponent];
NSURL *fileURLNew = [fileURLPrefix URLByAppendingPathComponent:fileNameNew];

BOOL move = [[NSFileManager defaultManager] moveItemAtURL:fileURLOld toURL:fileURLNew error:&errorMove];
if (!move) {
    //handle error
} else {
    //complete process
}
Was it helpful?

Solution

The NSString path manipulation methods like stringByAppendingPathComponent expects file paths not URLs. It will remove double slashes as this is not a valid file path (although it is a valid URL).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top