Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 尝试重命名文档目录中的文件时出现错误";文件:/…”;_Ios_Objective C_Nsstring_Nsfilemanager_Nsurl - Fatal编程技术网

Ios 尝试重命名文档目录中的文件时出现错误";文件:/…”;

Ios 尝试重命名文档目录中的文件时出现错误";文件:/…”;,ios,objective-c,nsstring,nsfilemanager,nsurl,Ios,Objective C,Nsstring,Nsfilemanager,Nsurl,下面是我编写的代码,用于更改iOS设备“Documents”目录中存储的任何文件的名称 使用此代码重命名文件时,前缀解析为 "file:/localhost/private/var/mobile/Applications/.../Documents/" 而不是格式正确的 "file://localhost/private/var/mobile/Applications/.../Documents/" 我丢失了一个斜杠,因此重命名方法失败 我已经为NSString和NSURL两种格式重写了这

下面是我编写的代码,用于更改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的帮助下,我已经调整了我的代码,并包含了下面的工作版本,以方便其他人

#

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

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

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

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

所以,您肯定使用了调试器来检查
/
在哪一行丢失了,是吗?只是在设备上重新检查了测试。斜杠在创建“filePath”变量时丢失。。。所以NSString*文件路径解析为“file:/”。。。因此moveItemAtPath:方法失败。调试器中的错误说明。。。Error Domain=NSCOCAERRORDOMAIN Code=4“操作无法完成。(Cocoa错误4.)”UserInfo=0x1da9a850{NSSourceFilePathErrorKey=file://localhost/private.../Documents/_documents_added/A0516%20%5BA%5D.pdf,NSUserStringVariant=(移动),NSFilePath=file://localhost/private.../Documents/A0516%20%5BA%5D.pdf,NSDestinationFilePath=file:/localhost/private…/Documents/A0516%20%5BA.pdf,NSUnderlyingError=0x1d915290“操作无法完成。没有这样的文件或目录”}因此,您肯定使用了调试器来检查
/
在哪一行丢失,是吗?只是对设备进行了双重检查。斜杠在创建“filePath”变量时丢失。。。所以NSString*文件路径解析为“file:/”。。。因此moveItemAtPath:方法失败。调试器中的错误说明。。。Error Domain=NSCOCAERRORDOMAIN Code=4“操作无法完成。(Cocoa错误4.)”UserInfo=0x1da9a850{NSSourceFilePathErrorKey=file://localhost/private.../Documents/_documents_added/A0516%20%5BA%5D.pdf,NSUserStringVariant=(移动),NSFilePath=file://localhost/private.../Documents/A0516%20%5BA%5D.pdf,NSDestinationFilePath=file:/localhost/private…/Documents/A0516%20%5BA.pdf,NSUnderlyingError=0x1d915290“操作无法完成。没有这样的文件或目录”}因此,您肯定使用了调试器来检查
/
在哪一行丢失,是吗?只是对设备进行了双重检查。斜杠在创建“filePath”变量时丢失。。。所以NSString*文件路径解析为“file:/”。。。因此moveItemAtPath:方法失败。调试器中的错误说明。。。Error Domain=NSCOCAERRORDOMAIN Code=4“操作无法完成。(Cocoa错误4.)”UserInfo=0x1da9a850{NSSourceFilePathErrorKey=file://localhost/private.../Documents/_documents_added/A0516%20%5BA%5D.pdf,NSUserStringVariant=(移动),NSFilePath=file://localhost/private.../Documents/A0516%20%5BA%5D.pdf,NSDestinationFilePath=file:/localhost/private…/Documents/A0516%20%5BA.pdf,NSUnderlyingError=0x1d915290“操作无法完成。没有这样的文件或目录”}因此,您肯定使用了调试器来检查
/
在哪一行丢失,是吗?只是对设备进行了双重检查。斜杠在创建“filePath”变量时丢失。。。所以NSString*文件路径解析为“file:/”。。。因此moveItemAtPath:方法失败。调试器中的错误说明。。。Error Domain=NSCOCAERRORDOMAIN Code=4“操作无法完成。(Cocoa错误4.)”UserInfo=0x1da9a850{NSSourceFilePathErrorKey=file://localhost/private.../Documents/_documents_added/A0516%20%5BA%5D.pdf,NSUserStringVariant=(移动),NSFilePath=file://localhost/private.../Documents/A0516%20%5BA%5D.pdf,NSDestinationFilePath=file:/localhost/private…/Documents/A0516%20%5BA.pdf,NSUnderlyingError=0x1d915290“操作无法完成。没有这样的文件或目录”}我还尝试使用以下代码:
NSURL*fileurold=[NSURLwithstring:document.dStringURL];
NSURL*fileURL=[NSURL-URLWithString:[document.dStringURL-stringByDeletingLastPathComponent];
NSURL*fileURLNew=[fileURL-urlbayappendingpathconment:filenamew];
BOOL-move=[[NSFileManager-defaultManager]MoveItemAttribute:fileURLOld Tour:fileURLNew error:&errorMove];
也有同样的错误……我没有领会你的意思吗?我想是的。你仍然在使用
stringByDeletingLastPathComponent
删除字符串中的“/”。首先使用
[NSURLwithString:document.dStringURL]将字符串转换为NSURL
。然后只使用NSURL方法来操作它。谢谢@Mario,一旦我对你的建议进行了更多的思考,这就帮助我走上了正确的道路。(我从同一方法中寻求两个不同的结果,并且混合了代码,这也混淆了问题。)正如你所建议的,我使用了
NSURLNSError *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
}