【问题标题】:Attempt to rename file in Documents directory has error "file:/..."尝试重命名 Documents 目录中的文件时出现错误“file:/...”
【发布时间】:2014-01-26 16:32:58
【问题描述】:

以下是我编写的代码,用于更改存储在 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
}

【问题讨论】:

  • 那么,您肯定使用过调试器来检查/ 在哪一行丢失了,是吗?
  • 刚刚在设备上仔细检查了测试。斜线在创建“filePath”变量时丢失了……所以 NSString *filePath 解析为“file:/”……所以 moveItemAtPath: 方法失败了。
  • 来自调试器的错误描述... 错误域=NSCocoaErrorDomain Code=4 “操作无法完成。(Cocoa 错误 4。)” UserInfo=0x1da9a850 {NSSourceFilePathErrorKey=file://localhost/私人.../Documents/_documents_added/A0516%20%5BA%5D.pdf, NSUserStringVariant=( Move ), NSFilePath=file://localhost/private.../Documents/A0516%20%5BA%5D.pdf, NSDestinationFilePath=file:/localhost/private.../Documents/A0516%20%5BA.pdf, NSUnderlyingError=0x1d915290 "操作无法完成。没有这样的文件或目录"}

标签: ios objective-c nsstring nsfilemanager nsurl


【解决方案1】:

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

【讨论】:

  • 我也尝试使用以下代码:NSURL *fileURLOld = [NSURL URLWithString:document.dStringURL];NSURL *fileURL = [NSURL URLWithString:[document.dStringURL stringByDeletingLastPathComponent]];NSURL *fileURLNew = [fileURL URLByAppendingPathComponent:fileNameNew];BOOL move = [[NSFileManager defaultManager] moveItemAtURL:fileURLOld toURL:fileURLNew error:&errorMove]; 出现同样的错误...我错过了你的意思吗?
  • 我想是的。您仍在使用 stringByDeletingLastPathComponent 删除字符串中的“//”。首先使用[NSURL URLWithString:document.dStringURL] 将字符串转换为NSURL。然后只使用 NSURL 方法来操作它。
  • 感谢@Mario,一旦我对您的建议进行了更多思考,这帮助我走上了正确的道路。 (我正在从同一方法中寻求两个不同的结果,并且正在混合代码,这也使问题变得混乱。)正如你所建议的,我已经使用NSURL 方法来操作 URL“字符串”,然后正如你所建议的那样转换为针对实体的存储属性在核心数据中。我在下面包含了我的更新代码以帮助其他人...
猜你喜欢
  • 1970-01-01
  • 2020-12-26
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 2014-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多