Delete files in iOS directory using Swift

前端 未结 2 698
有刺的猬
有刺的猬 2021-01-31 03:15

I downloaded some PDF files in my app and want to delete these on closing the application.

For some reason it does not work:

Creating the file:

l         


        
2条回答
  •  甜味超标
    2021-01-31 03:51

    I believe your problem is on this line:

    let filePaths = try fileManager.contentsOfDirectoryAtPath("\(documentsUrl)")
    

    You're using contentsOfDirectoryAtPath() with something that is an NSURL. You choose either path strings or URLs, not try to mix them both. To pre-empty your possible next question, URLs are preferred. Try using contentsOfDirectoryAtURL() and removeItemAtURL().

    Another curious thing you should look at once you resolve the above: why are you using NSTemporaryDirectory() for the file path when you try to delete? You're reading the document directory and should use that.

提交回复
热议问题