NSSearchPathForDirectoriesInDomains解释混淆 [英] NSSearchPathForDirectoriesInDomains explanation confused
问题描述
我刚才一直在研究这个code,它检查文件是否存在:
的NSString *路径;
NSArray的*路径= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
路径= [[路径objectAtIndex:0] stringByAppendingPathComponent:@SomeDirectory];
路径= [路径stringByAppendingPathComponent:@SomeFileName];
如果([的NSFileManager defaultManager] fileExistsAtPath:路径])
{
但我有点困惑。通过以下行:
的NSArray *路径= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
好吧,我明白的方法NSSearchPathForDirectoriesInDomains返回,这取决于你传递到该方法的参数的路径。但这个用户(谁写的code)是一味传递一个全班同学! (指的是NSDocumentDirectory,NSUserDOmainMask)。他正确地传递的唯一一件事就是BOOL YES。我检查了苹果的文档,它说的:
NSSearchPathForDirectoriesInDomains创建的目录搜索路径列表。
的NSArray * NSSearchPathForDirectoriesInDomains(
NSSearchPathDirectory目录,
NSSearchPathDomainMask domainMask,
BOOL expandTilde
);
我在苹果的文档搜索NSSearchPathDirectory和NSSearchPathDomainMask他们建议我一定要通过一些
这表明需要传递到方法是多少?有人能解释一下行吗?谢谢
在这里Foundation框架常量阅读文档:结果
<一href=\"http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html#//apple_ref/doc/c_ref/NSDocumentDirectory\">http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html#//apple_ref/doc/c_ref/NSDocumentDirectory
这是正确的使用,因为这些都不是类,但常量!
NSSearchPathDomainMask
搜索路径域指定常量为NSSearchPathDirectory式底座的位置。
块引用>{枚举
NSUserDomainMask = 1,//这一个
NSLocalDomainMask = 2,
NSNetworkDomainMask = 4,
NSSystemDomainMask = 8,
NSAllDomainsMask = 0x0ffff,
};
的typedef NSUInteger NSSearchPathDomainMask;NSSearchPathDirectory
这些常量指定各种目录的位置。
块引用>{枚举
NSApplicationDirectory = 1,
NSDemoApplicationDirectory,
NSDeveloperApplicationDirectory,
NSAdminApplicationDirectory,
NSLibraryDirectory,
NSDeveloperDirectory,
NSUserDirectory,
NSDocumentationDirectory,
NSDocumentDirectory,//这一个
NSCoreServiceDirectory,
NSAutosavedInformationDirectory = 11,
NSDesktopDirectory = 12,
NSCachesDirectory = 13,
NSApplicationSupportDirectory = 14,
NSDownloadsDirectory = 15,
NSInputMethodsDirectory = 16,
NSMoviesDirectory = 17,
NSMusicDirectory = 18,
NSPicturesDirectory = 19,
NSPrinterDescriptionDirectory = 20,
NSSharedPublicDirectory = 21,
NS preferencePanesDirectory = 22,
NSItemReplacementDirectory = 99,
NSAllApplicationsDirectory = 100,
NSAllLibrariesDirectory = 101
};
的typedef NSUInteger NSSearchPathDirectory;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
解决方案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/NSDocumentDirectoryIt'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;
这篇关于NSSearchPathForDirectoriesInDomains解释混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!