gpt4 book ai didi

iphone - NSSearchPathForDirectoriesInDomains 解释混淆

转载 作者:太空宇宙 更新时间:2023-11-04 00:19:16 26 4
gpt4 key购买 nike

我刚刚研究了检查文件是否存在的代码:

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
);

我在苹果文档中搜索了 NSSearchPathDirectory 和 NSSearchPathDomainMask,他们建议我必须传递一个数字

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

最佳答案

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

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


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;

关于iphone - NSSearchPathForDirectoriesInDomains 解释混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7627177/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com