On May 10, 2014, at 12:06 , William Squires <wsqui...@satx.rr.com> wrote:
> How can I make sure MyFile.txt exists before trying to read it in? You can’t, so don’t even try. :) Because the file system gets modified asynchronously by other processes, there’s always a potential race condition between checking for existence and using the result of the check. IOW, the existence check is out of date as soon as its done. The correct strategy is to go ahead and read the file. If the read fails, look at the error code, and deal with the “file does not exist” error then. Incidentally, if you did want to check if a file exists (let’s say, if you have a list of files in your UI and you wanted to gray out the ones that don’t exist right now), I’d recommend you: — determine the directory explicitly — construct a file URL using the directory and file name — get one of the URL file resources (such as the file name) for that URL — if an error results, treat that as “file does not exist” You can check the error for an explicit “file does not exist” code, if it really, but that still leaves you with other potential errors that you need to deal with somehow. You’ll likely want to do the same thing as if the file really doesn’t exist (such as gray out its name, in the above example). P.S. Fritz beat me to the punch on replying, but I’m posting anyway because I’d like to *strongly* recommend that you use a URL-based approach, and not a path-based approach. -[NSFileManager fileExistsAtPath:] is pretty much legacy API nowadays (and it’s significant that there’s no URL-based version of it). It’s worth training yourself to use URLs over paths. _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com