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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:11:49  来源:igfitidea点击:

iOS copy directories including subdirectories

iphoneobjective-ciosxcodecocoa-touch

提问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 NSFileManagermethods 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:&copyError]) {
    NSLog(@"Error copying files: %@", [copyError localizedDescription]);
}

回答by rob mayoff

The NSFileManagermethods 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: