使用 moveItemAtPath 将文件夹移动到临时文件夹时出现奇怪的错误。可可错误516
我尝试将包含文件的文件夹移动到临时文件夹,但总是收到相同的错误: 操作无法完成。 (可可错误516。)
这是代码,你看到什么奇怪的地方了吗?提前致谢。
// create new folder
NSString* newPath=[[self getDocumentsDirectory] stringByAppendingPathComponent:@"algo_bueno"];
NSLog(@"newPath %@", newPath);
if ([[NSFileManager defaultManager] fileExistsAtPath:newPath]) {
NSLog(@"newPath already exists.");
} else {
NSError *error;
if ([[NSFileManager defaultManager] createDirectoryAtPath:newPath withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@"newPath created.");
} else {
NSLog(@"Unable to create directory: %@", error.localizedDescription);
return;
}
}
// create a file in that folder
NSError *error;
NSString* myString=[NSString stringWithFormat:@"Probando..."];
NSString* filePath=[newPath stringByAppendingPathComponent:@"myfile.txt"];
if ([myString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]) {
NSLog(@"File created.");
} else {
NSLog(@"Failed creating file. %@", error.localizedDescription);
return;
}
// move this folder and its folder
NSString *tmpDir = NSTemporaryDirectory();
NSLog(@"temporary directory, %@", tmpDir);
if ([[NSFileManager defaultManager] moveItemAtPath:newPath toPath:tmpDir error:&error]) {
NSLog(@"Movido a temp correctamente");
} else {
NSLog(@"Failed moving to temp. %@", error.localizedDescription);
}
I try to move a folder with a file to a temp folder, but I always receive the same error:
The operation couldn’t be completed. (Cocoa error 516.)
This is the code, do you see anything strange? Thanks in advance.
// create new folder
NSString* newPath=[[self getDocumentsDirectory] stringByAppendingPathComponent:@"algo_bueno"];
NSLog(@"newPath %@", newPath);
if ([[NSFileManager defaultManager] fileExistsAtPath:newPath]) {
NSLog(@"newPath already exists.");
} else {
NSError *error;
if ([[NSFileManager defaultManager] createDirectoryAtPath:newPath withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@"newPath created.");
} else {
NSLog(@"Unable to create directory: %@", error.localizedDescription);
return;
}
}
// create a file in that folder
NSError *error;
NSString* myString=[NSString stringWithFormat:@"Probando..."];
NSString* filePath=[newPath stringByAppendingPathComponent:@"myfile.txt"];
if ([myString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]) {
NSLog(@"File created.");
} else {
NSLog(@"Failed creating file. %@", error.localizedDescription);
return;
}
// move this folder and its folder
NSString *tmpDir = NSTemporaryDirectory();
NSLog(@"temporary directory, %@", tmpDir);
if ([[NSFileManager defaultManager] moveItemAtPath:newPath toPath:tmpDir error:&error]) {
NSLog(@"Movido a temp correctamente");
} else {
NSLog(@"Failed moving to temp. %@", error.localizedDescription);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
错误 516 是
NSFileWriteFileExistsError
您无法将文件移动到文件已存在的位置:)
(请参阅此处的文档 - https://developer.apple.com/library/mac /#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html 并搜索“516”)
更多有用的是,您的目标路径应该是文件名,而不是文件夹。试试这个:
Error 516 is
NSFileWriteFileExistsError
You can't move a file to a place where a file already exists :)
(See docs here - https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html and search for '516')
More usefully, your destination path should be a file name, not a folder. Try this :
使用以下方法获取唯一的临时文件夹名称:
NSFileManager 唯一文件名
Obtain a unique temp folder name by using the following method:
NSFileManager unique file names
moveItemAtPath:toPath:error:
的目标路径必须是要移动的文件或目录的完整新路径。正如您现在所做的那样,您基本上是在尝试用您的文件覆盖临时目录本身。简单修复:
The target path for
moveItemAtPath:toPath:error:
has to be the complete new path of the file or directory you're moving. As you do it now, you're basically trying to overwrite the temp directory itself with your file.Simple fix: