1
votes

I have just been studying this code which checks if a file exists:

NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectory"];
path = [path stringByAppendingPathComponent:@"SomeFileName"];
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
{

but I'm a bit confused. by the following line:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

Ok I Understand that the Method NSSearchPathForDirectoriesInDomains returns a path depending on the arguments which you pass into this method. But this user (whoever wrote the code) is blindly passing in a whole class! (refering to NSDocumentDirectory, NSUserDOmainMask). The only thing he pass correctly is the BOOL YES. I check the apple docs and it says this:

NSSearchPathForDirectoriesInDomains Creates a list of directory search paths.

NSArray * NSSearchPathForDirectoriesInDomains (
NSSearchPathDirectory directory,
NSSearchPathDomainMask domainMask,
BOOL expandTilde
);

I have search for NSSearchPathDirectory and NSSearchPathDomainMask in apple docs and they suggest I have to pass a number

This would suggest a number needs to be passed into the method? Can somebody explain that line please? thanks

1

1 Answers

12
votes

Read documentation of Foundation framework constants here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html#//apple_ref/doc/c_ref/NSDocumentDirectory

It's correct usage, because those are not classes but constants!


NSSearchPathDomainMask

Search path domain constants specifying base locations for the NSSearchPathDirectory type.

enum {
   NSUserDomainMask = 1, //this one
   NSLocalDomainMask = 2,
   NSNetworkDomainMask = 4,
   NSSystemDomainMask = 8,
   NSAllDomainsMask = 0x0ffff,
};
typedef NSUInteger NSSearchPathDomainMask;

NSSearchPathDirectory

These constants specify the location of a variety of directories.

enum {
   NSApplicationDirectory = 1,
   NSDemoApplicationDirectory,
   NSDeveloperApplicationDirectory,
   NSAdminApplicationDirectory,
   NSLibraryDirectory,
   NSDeveloperDirectory,
   NSUserDirectory,
   NSDocumentationDirectory,
   NSDocumentDirectory,  // this one
   NSCoreServiceDirectory,
   NSAutosavedInformationDirectory = 11,
   NSDesktopDirectory = 12,
   NSCachesDirectory = 13,
   NSApplicationSupportDirectory = 14,
   NSDownloadsDirectory = 15,
   NSInputMethodsDirectory = 16,
   NSMoviesDirectory = 17,
   NSMusicDirectory = 18,
   NSPicturesDirectory = 19,
   NSPrinterDescriptionDirectory = 20,
   NSSharedPublicDirectory = 21,
   NSPreferencePanesDirectory = 22,
   NSItemReplacementDirectory = 99,
   NSAllApplicationsDirectory = 100,
   NSAllLibrariesDirectory = 101
};
typedef NSUInteger NSSearchPathDirectory;