如何在 swift3 中删除 .DS_Store

How to remove .DS_Store in swift3

您好commonPath是我的文档目录路径。我必须删除目录中的 .DS_Store 文件。

在 swift 2 中,我使用了下面的代码,它工作正常

var imageNames = try! NSFileManager.defaultManager().contentsOfDirectoryAtPath(commonPath)

                if imageNames.count > 0
                {
                    imageNames.contains(".DS_Store") ? imageNames.removeAtIndex(imageNames.indexOf(".DS_Store")!) : imageNames[0]
                }

但我无法在 swift 3.0

中使用相同的代码

Xcode 将上面的代码自动转换为

imageNames.contains(".DS_Store") ? imageNames.remove(at: imageNames.index(of: ".DS_Store")!) : imageNames[0]

显示错误。 谁能帮帮我?

提前致谢。

像这样的东西应该在 swift 3:

中工作
var imageNames = try! FileManager.default.contentsOfDirectory(atPath: commonPath)

if imageNames.count > 0 {
    if imageNames.contains(".DS_Store") {
        imageNames.remove(at: imageNames.index(of: ".DS_Store")!)
    }
}
// do something with imageNames[0]

使用 NSSearchPathForDirectoriesInDomains 获取文档路径,然后执行以下操作。希望有用。

let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString

let commonPath = documentPath.appendingPathComponent(reportImageFolder)
let outPath = commonPath + "/"

arrImages = try FileManager.default.contentsOfDirectory(atPath: outPath as String)

  if arrImages.count > 0
   {
      arrImages.contains(".DS_Store") ? arrImages.remove(at: arrImages.index(of: ".DS_Store")!) : arrImages[0]
   }