xcode iOS 复制目录,包括子目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9119940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS copy directories including subdirectories
提问by Jaume
I am working with iOS document folder called "temp" that allocates subdirectories that contain files dowloaded from remote url. Now I need to copy "temp" directory and all its contents to "main" folder (overwrite existing that was previously created). I know how to handle files but how to copy whole directory? Thank you
我正在使用名为“temp”的 iOS 文档文件夹,该文件夹分配包含从远程 url 下载的文件的子目录。现在我需要将“temp”目录及其所有内容复制到“main”文件夹(覆盖先前创建的现有文件夹)。我知道如何处理文件但如何复制整个目录?谢谢
回答by jrtc27
You can use the same NSFileManager
methods that you know and love for directories as well. For example:
您也可以使用NSFileManager
您熟悉和喜爱的目录方法。例如:
if ([[NSFileManager defaultManager] fileExistsAtPath:pathToTempDirectoryInMain]) {
[[NSFileManager defaultManager] removeItemAtPath:pathToTempDirectoryInMain error:nil];
}
NSError *copyError = nil;
if (![[NSFileManager defaultManager] copyItemAtPath:pathToTempDirectory toPath:pathToTempDirectoryInMain error:©Error]) {
NSLog(@"Error copying files: %@", [copyError localizedDescription]);
}
回答by rob mayoff
The NSFileManager
methods removeItemAtPath:error:
, removeItemAtURL:error:
, copyItemAtPath:toPath:error:
, and copyItemAtURL:toURL:error:
methods handle directories.
该NSFileManager
方法removeItemAtPath:error:
,removeItemAtURL:error:
,copyItemAtPath:toPath:error:
和copyItemAtURL:toURL:error:
方法处理目录。
You might also look at moveItemAtPath:toPath:error:
and moveItemAtURL:toURL:error:
.
您还可以查看moveItemAtPath:toPath:error:
和moveItemAtURL:toURL:error:
。