How can I fix the following errors when running the code below? I already searched in SO but can't find anything for Swift 3.
// Delete all files in given directory
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let dataPath = NSURL(fileURLWithPath: path)
if let enumerator = FileManager.default.enumerator(atPath: dataPath.path!) {
while let fileName = enumerator.nextObject() as? String {
do {
try FileManager.default.removeItem(atPath: "\(dataPath)\(fileName)")
}
catch let e as NSError {
print(e)
}
catch {
print("error")
}
}
}
Log:
Error Domain=NSCocoaErrorDomain Code=4 "“.DS_Store” couldn’t be removed." UserInfo={NSFilePath=file:///Users/CIPL0469/Library/Developer/CoreSimulator/Devices/F0106B28-C4D1-4FE2-A425-D04C6BFDDC01/data/Containers/Data/Application/A5AB9B7B-6174-4BA7-9EFD-0E9F1C98CB17/Documents/.DS_Store, NSUserStringVariant=( Remove ), NSUnderlyingError=0x60800004fed0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
Error Domain=NSCocoaErrorDomain Code=4 "“2017-01-31_10-44-21.m4a” couldn’t be removed." UserInfo={NSFilePath=file:///Users/CIPL0469/Library/Developer/CoreSimulator/Devices/F0106B28-C4D1-4FE2-A425-D04C6BFDDC01/data/Containers/Data/Application/A5AB9B7B-6174-4BA7-9EFD-0E9F1C98CB17/Documents/2017-01-31_10-44-21.m4a, NSUserStringVariant=( Remove ), NSUnderlyingError=0x60800004fa80 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
Error Domain=NSCocoaErrorDomain Code=4 "“2017-01-31_10-44-26.m4a” couldn’t be removed." UserInfo={NSFilePath=file:///Users/CIPL0469/Library/Developer/CoreSimulator/Devices/F0106B28-C4D1-4FE2-A425-D04C6BFDDC01/data/Containers/Data/Application/A5AB9B7B-6174-4BA7-9EFD-0E9F1C98CB17/Documents/2017-01-31_10-44-26.m4a, NSUserStringVariant=( Remove ), NSUnderlyingError=0x60000004f570 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}