Testing file existence using NSURL
Posted
by Peter Hosey
on Stack Overflow
See other posts from Stack Overflow
or by Peter Hosey
Published on 2009-12-18T11:45:51Z
Indexed on
2010/04/24
12:03 UTC
Read the original article
Hit count: 310
Snow Leopard introduced many new methods to use NSURL objects to refer to files, not pathnames or Core Services' FSRefs.
However, there's one task I can't find a URL-based method for: Testing whether a file exists. I'm looking for a URL-based version of -[NSFileManager fileExistsAtPath:
]. Like that method, it should return YES
if the URL describes anything, whether it's a regular file, a directory, or anything else.
I could attempt to look up various resource values, but none of them are explicitly guaranteed to not exist if the file doesn't, and some of them (e.g., NSURLEffectiveIconKey) could be costly if it does.
I could just use NSFileManager's fileExistsAtPath:
, but if there's a more modern method, I'd prefer to use that.
Is there a simple method or function in Cocoa, CF, or Core Services that's guaranteed/documented to tell me whether a given file (or file-reference) URL refers to a file-system object that exists?
© Stack Overflow or respective owner