如何删除swift3中的.DS_存储
您好这里如何删除swift3中的.DS_存储,swift3,nsfilemanager,Swift3,Nsfilemanager,您好这里commonPath是我的文档目录路径。我必须删除目录中的.DS\u Store文件 在swift 2中,我使用了下面的代码,它运行良好 var imageNames = try! NSFileManager.defaultManager().contentsOfDirectoryAtPath(commonPath) if imageNames.count > 0 { imag
commonPath
是我的文档目录路径。我必须删除目录中的.DS\u 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]
}