简体   繁体   中英

Cannot get completion result when removing file using NSFileManager in Swift

I'm trying to remove a video that the user recorded and now has decided to delete. I have both a file URL and then obviously a path too.

I have tried using the removal methods of the NSFileManager class for both the file and the path, but I'm having trouble getting a completion result to confirm whether the file has actually been deleted or not.

Here is an example of how I'm trying to remove the file in Swift:

let deleted = try! NSFileManager.defaultManager().removeItemAtURL(self.fileURL)

This will give me a warning of Constant 'deleted' inferred to have type '()', which may be unexpected

Using removeItemAtPath produces the same warning. If I run the code, deleted simply logs as ()

If I look at the method signatures for these two methods it's clear that they do not return a result, but take for example the documentation for the removeItemAtURL method: true if the item was removed successfully or if URL was nil. Returns false if an error occurred. If the delegate aborts the operation for a file, this method returns true. However, if the delegate aborts the operation for a directory, this method returns false. true if the item was removed successfully or if URL was nil. Returns false if an error occurred. If the delegate aborts the operation for a file, this method returns true. However, if the delegate aborts the operation for a directory, this method returns false.

It also mentions taking an error parameter but doesn't have one. And then finally in the last sentence it says: Returns YES if the item was removed successfully or if URL was nil.

As a last resort I figured I could just become the delegate for NSFileManager , but it's delegate protocol does not offer any completion methods.

How can I properly remove a file or path and then verify that it has actually been deleted?

You're right that in Swift it returns Void (I believe the docs include the return value description for when you have Objective-C or Both turned on, as opposed to just Swift -- they've got a lot of work to do updating the docs).

If you continue reading the documentation for that function, you'll see a section titled "Handling Errors in Swift", which says:

In Swift, this method returns Void and is marked with the throws keyword to indicate that it throws an error in cases of failure.

You call this method in a try expression and handle any errors in the catch clauses of a do statement, as described in Error Handling in The Swift Programming Language (Swift 2.1) and Error Handling in Using Swift with Cocoa and Objective-C (Swift 2.1).

So wrap your call in a try/catch and handle the error if there is one. If there wasn't an error, it succeeded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM