【发布时间】:2014-01-26 16:32:58
【问题描述】:
以下是我编写的代码,用于更改存储在 iOS 设备“Documents”目录中的任何文件的名称。
使用此代码重命名文件时,前缀解析为...
"file:/localhost/private/var/mobile/Applications/.../Documents/"
而不是正确的格式...
"file://localhost/private/var/mobile/Applications/.../Documents/"
我失去了一个斜线,所以重命名方法失败了!!!
我已经为NSString 和NSURL 格式重写了这个,两种方法都出现了相同的错误。我已经使用 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