NSSearchPathForDirectoriesInDomains解释混乱

时间:2011-10-02 15:12:44

标签: iphone c xcode file

我刚学习这段代码,检查文件是否存在:

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

但我有点困惑。通过以下行:

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

确定我理解方法NSSearchPathForDirectoriesInDomains根据您传递给此方法的参数返回路径。但是这个用户(编写代码的人)盲目地传递了全班! (参考NSDocumentDirectory,NSUserDOmainMask)。他唯一正确传递的是BOOL YES。我检查了苹果文档,它说:

NSSearchPathForDirectoriesInDomains创建目录搜索路径列表。

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

我在Apple docs中搜索了NSSearchPathDirectory和NSSearchPathDomainMask,他们建议我必须传递一个数字

这表明需要将一个数字传递给方法?有人可以解释一下这条线吗?感谢

1 个答案:

答案 0 :(得分:11)

在此处阅读Foundation框架常量的文档:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html#//apple_ref/doc/c_ref/NSDocumentDirectory

这是正确的用法,因为那些不是类而是常量!


NSSearchPathDomainMask

  

搜索路径域常量,指定NSSearchPathDirectory类型的基本位置。

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

NSSearchPathDirectory

  

这些常量指定各种目录的位置。

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;