我想把一个没有文件的文件夹复制到一个新的位置。如果我只是按名称新建它,我将错过所有属性。是否有复制功能(包括访问权限、色标等)?

最佳答案

创建克隆时,可以设置克隆的属性:

let targetPath = "..." as NSString
let destinationPath = "..." as NSString

let fileManager = NSFileManager.defaultManager()
let enumerator = fileManager.enumeratorAtPath(targetPath as String)!

for item in enumerator {
    let fullTargetPath = targetPath.stringByAppendingPathComponent(item as! String)
    let attributes = try! fileManager.attributesOfItemAtPath(fullTargetPath)

    if let fileType = attributes[NSFileType] as? String where fileType == NSFileTypeDirectory {
        let fullDestinationPath = destinationPath.stringByAppendingPathComponent(item as! String)
        try! fileManager.createDirectoryAtPath(fullDestinationPath, withIntermediateDirectories: true, attributes: attributes)
    }
}

关于swift - Swift:复制文件夹/目录而不包含文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39270307/

10-14 20:15
查看更多