尝试重命名文档目录中的文件有错误“文件:/ ...”

时间:2014-01-08 14:08:00

标签: ios objective-c nsstring nsfilemanager nsurl

下面是我编写的用于更改存储在iOS设备“Documents”目录中的任何文件名称的代码。

使用此代码重命名文件时,前缀解析为...

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

而不是格式正确的......

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

我正在丢失斜杠,因此重命名方法失败!!!

我已经为NSStringNSURL格式重写了这个,并且这两种方法都存在相同的错误。我已经使用iOS模拟器和设备对此进行了测试,两个测试都出现了相同的错误。

我怀疑我忽略了一些简单的东西,但无法弄清楚它是什么 - 请帮忙吗?

- (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
    }
}

在@Mario的帮助下,我调整了我的代码并将下面的工作版本包含在内,以造福他人......

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
}

1 个答案:

答案 0 :(得分:3)

stringByAppendingPathComponent这样的NSString路径操作方法需要文件路径而不是URL。它将删除双斜杠,因为这不是有效的文件路径(尽管它是有效的URL)。