Verifying file existence using NSURL

Snow Leopard introduced many new methods for using NSURL objects to access files rather than path names or FSRefs of core services.

However, there is one task: I cannot find a method based on the URL for: Checking for a file. I am looking for the URL version - [NSFileManager fileExistsAtPath: ] . Like this method, it should return YES if the URL describes something, be it a regular file, directory, or something else.

I could try to find different resource values , but none of them explicitly guaranteed to exist if the file is missing, and some of them (like NSURLEffectiveIconKey ) can be expensive if that happens.

I could just use NSFileManager fileExistsAtPath: but if there is a more modern method, I would prefer to use it.

Is there an easy way or function in Cocoa, CF, or basic services that guarantee / document to tell me if a given file (or file link) refers to a file system object?

+67
cocoa core-foundation osx-snow-leopard nsurl nsfilemanager
Dec 18 '09 at 11:45
source share
5 answers

NSURL has this method:

 - (BOOL)checkResourceIsReachableAndReturnError:(NSError **)error 

Which "Returns whether the resource pointed to by the file URL is available."

 NSURL *theURL = [NSURL fileURLWithPath:@"/Users/elisevanlooij/nonexistingfile.php" isDirectory:NO]; NSError *err; if ([theURL checkResourceIsReachableAndReturnError:&err] == NO) [[NSAlert alertWithError:err] runModal]; 
+130
Dec 18 '09 at 16:16
source share

In iOS, I did not find another way ...

 NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"file.type"]; if ([[NSFileManager defaultManager] fileExistsAtPath:[storeURL path]]) {...} 
+29
Jan 20 2018-11-11T00:
source share

Here is Swift 2 answer:

 var error:NSError? let folderExists = theURL.checkResourceIsReachableAndReturnError(&error) 
+9
Jan 28 '16 at 20:11
source share

Determining whether a given file (or file link) is a reference to a file system object that exists is initially expensive for remote resources, only 10.6 (without iPhoneOS) api for this CFURLResourceIsReachable () and [NSURL checkResourceIsReachableAndReturnError:] are both synchronous. even if you use them, for a large number of files you will still look at significant delays in overhead.

What you need to do is implement your own asynchronous cached validation procedure, which separately creates a list of valid resources.

Otherwise, the notes for CFURLResourceIsReachable are in the header state:

An example is the periodic maintenance of the state of the user interface, which depends on the existence of a particular document. When performing such as opening a file, it is more efficient to simply try operations and processing failures than to first check for availability.

+2
Apr 24 '10 at 11:53
source share

Since NSURL can represent more local file systems, I don't think there is a general method that can verify their existence in a reliable way. At least Cocoa's foundation does not contain such a function (as far as I know).

If you are dealing only with local file systems, I suggest you create a category for NSURL or for NSFileManager with the message urlExists: It converts NSURL to NSString (normalized path), and then calls the message [NSFileManager fileExistsAtPath:] .

+1
Dec 18 '09 at 12:06
source share



All Articles