[英]iOS/Swift 4: How to copy a folder containing several folders containing files without removing the files and folders existing destination?
I would like to copy a folder containing several folders, and this folders contain several files. 我想复制一个包含几个文件夹的文件夹,并且该文件夹包含几个文件。 If this folders are existing, I would like to keep them inside, and overriding the files if existing. 如果此文件夹存在,我想将它们保留在其中,并覆盖文件(如果存在)。 If the files doesn't exist, they are simply naturally added. 如果文件不存在,则自然地添加它们。
_ = try FileManager.default.replaceItemAt(previousItemUrl, withItemAt: currentItemUrl)
For example: 例如:
Files existing on device: 设备上现有的文件:
APP/XXX/a.txt
APP/XXX/b.txt
APP/XXX/c.txt
APP/YYY/d.txt
APP/ZZZ/e.txt
APP/ZZZ/f.txt
Files to copy: 要复制的文件:
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
Do I have to create my self a recursive function, or did that function already exists natively? 我必须创建自己的递归函数,还是该函数本身已经存在?
Thanks in advance. 提前致谢。
We can use below native method. 我们可以使用下面的本机方法。 It copies the source to destination. 它将源复制到目标。
func copyItem(atPath srcPath: String,
toPath dstPath: String) throws
We can also control the override use case by implementing other delegate methods mentioned below. 我们还可以通过实现下面提到的其他委托方法来控制替代用例。
optional func fileManager(_ fileManager: FileManager,
shouldCopyItemAt srcURL: URL,
to dstURL: URL) -> Bool
You can find all these details in Apple document 您可以在Apple文档中找到所有这些详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.