【问题标题】:Swift: Copy folder/directory without filesSwift:复制没有文件的文件夹/目录
【发布时间】:2017-01-09 06:22:54
【问题描述】:

我想将没有文件的文件夹复制到新位置。如果我只是按名称重新创建它,我会错过所有属性。有没有可以复制的功能(包括访问权限、颜色标签等...)?

【问题讨论】:

    标签: swift macos copy directory


    【解决方案1】:

    您可以在创建克隆时设置它们的属性:

    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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-03
      • 2017-04-28
      • 2012-06-17
      • 2011-08-24
      • 1970-01-01
      • 2014-07-25
      • 2021-04-27
      • 1970-01-01
      • 2013-10-22
      相关资源
      最近更新 更多