Search code examples
swiftmacosnsfilemanager

Where do files go when you delete them programatically with swift?


Where do files go when you delete them programatically? I deleted them with this code, but the Trash for today is empty. Is it possible to retrieve them?

let filemgr = NSFileManager.defaultManager()
        do{
            let filelist = try filemgr.contentsOfDirectoryAtPath(fontFolderPath)
            for filename in filelist {
                do{ try filemgr.removeItemAtPath(fontFolderPath+filename)} catch{}
            }
        }catch{}

Solution

  • Using the URL related API of NSFileManager you have two options:

    • func removeItemAtURL(_ URL: NSURL) throws

      deletes the item immediately like /bin/rm in Terminal.app and has the same functionality as removeItemAtPath.

    • func trashItemAtURL(_ url: NSURL, resultingItemURL outResultingURL: AutoreleasingUnsafeMutablePointer<NSURL?>) throws

      moves the item to the trash folder returning the item’s location in the trash via the inout pointer.