【发布时间】:2017-01-09 06:22:54
【问题描述】:
我想将没有文件的文件夹复制到新位置。如果我只是按名称重新创建它,我会错过所有属性。有没有可以复制的功能(包括访问权限、颜色标签等...)?
【问题讨论】:
标签: swift macos copy directory
我想将没有文件的文件夹复制到新位置。如果我只是按名称重新创建它,我会错过所有属性。有没有可以复制的功能(包括访问权限、颜色标签等...)?
【问题讨论】:
标签: swift macos copy directory
您可以在创建克隆时设置它们的属性:
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)
}
}
【讨论】: