iOS/swift 4 : How to copy a folder containing several folders containing files without removing the files and folders existing destination?

标签 ios swift file nsfilemanager

我想复制一个包含多个文件夹的文件夹,并且该文件夹包含多个文件。如果此文件夹存在,我想将它们保留在里面,并覆盖这些文件(如果存在)。如果文件不存在,则会自然添加它们。

_ = try FileManager.default.replaceItemAt(previousItemUrl, withItemAt: currentItemUrl)

例如:

设备上现有的文件:

APP/XXX/a.txt
APP/XXX/b.txt
APP/XXX/c.txt
APP/YYY/d.txt
APP/ZZZ/e.txt
APP/ZZZ/f.txt

要复制的文件:

APP/XXX/c.txt //Will override
APP/YYY/g.txt //new file added
APP/ZZZ/h.txt //new file added
APP/AAA       //new folder added

我是否必须为自己创建一个递归函数,或者该函数本身已经存在吗?

提前致谢。

最佳答案

我们可以使用下面的 native 方法。它将源复制到目标。

func copyItem(atPath srcPath: String, 
   toPath dstPath: String) throws

我们还可以通过实现下面提到的其他委托(delegate)方法来控制覆盖用例。

optional func fileManager(_ fileManager: FileManager, 
         shouldCopyItemAt srcURL: URL, 
                       to dstURL: URL) -> Bool

您可以在 Apple document 中找到所有这些详细信息

关于iOS/swift 4 : How to copy a folder containing several folders containing files without removing the files and folders existing destination?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55146167/

相关文章:

c - 在C中写入文件多线程

ios - Application Loader 由于可执行名称拒绝二进制文件

ios - Swift:导入自定义框架 - 使用未解析的标识符

Linux 文件和进程级 I/O 性能指标

node.js - 如何使用 Restify 提供静态文件

swift - CoreML 图像检测

ios - 带有 Xamarin ipa 的 AirWatch MDM

ios - Xcode Crash IOS APP:dyld:未加载库,原因:未找到图像

xcode - 从 Swift 中的 TableView 单元格访问数据

swift - RealmSwift 替代 RLMobject