gpt4 book ai didi

ios - 尝试重命名 Documents 目录中的文件时出现错误 "file:/..."

转载 作者:行者123 更新时间:2023-11-28 20:09:04 25 4
gpt4 key购买 nike

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

最佳答案

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

关于ios - 尝试重命名 Documents 目录中的文件时出现错误 "file:/...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20997955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com