我想复制一个包含多个文件夹的文件夹,并且该文件夹包含多个文件。如果此文件夹存在,我想将它们保留在里面,并覆盖这些文件(如果存在)。如果文件不存在,则会自然添加它们。
_ = 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/