Search code examples
swift3nsfilemanager

How to remove .DS_Store in swift3


Hi here commonPath is my document directory path. I have to remove .DS_Store file in my directory.

In swift 2 I used below code it works fine

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

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

But I can't able to use the same code in swift 3.0

Xcode convert automatically above code to

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

It shows error. Can anyone help me?

Thanks in advance.


Solution

  • Get document path using NSSearchPathForDirectoriesInDomains then do the following thing . Hope it will work.

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