在开发中,我们经常需要访问文件目录,而访问目录的方法里很常用的一个方法叫:
- URLForDirectory:inDomain:appropriateForURL:create:error:
官方的Quick help里这么描述这个方法:Locates and optionally creates the specified common directory in a domain. 中文意思大概为:定位和选择创建指定的常见的目录域
这个方法有五个参数,官方介绍如下:
directory:The search path directory. The supported values are described in NSSearchPathDirectory
. 翻译后为:搜索路径目录,支持的值相关描述在NSSearchPathDirectory中,点开NSSearchPathDirectory后发现这是一个枚举:
enum NSSearchPathDirectory : UInt {
case ApplicationDirectory
case DemoApplicationDirectory
case DeveloperApplicationDirectory
case AdminApplicationDirectory
case LibraryDirectory
case DeveloperDirectory
case UserDirectory
case DocumentationDirectory
case DocumentDirectory
case CoreServiceDirectory
case AutosavedInformationDirectory
case DesktopDirectory
case CachesDirectory
case ApplicationSupportDirectory
case DownloadsDirectory
case InputMethodsDirectory
case MoviesDirectory
case MusicDirectory
case PicturesDirectory
case PrinterDescriptionDirectory
case SharedPublicDirectory
case PreferencePanesDirectory
case ApplicationScriptsDirectory
case ItemReplacementDirectory
case AllApplicationsDirectory
case AllLibrariesDirectory
case TrashDirectory
}
这个参数里面的参数限定了这个方法中的目录,比较常用的有沙盒里的几个目录,比如DocumentationDirectory,CachesDirectory等等.
domain:The file system domain to search. The value for this parameter is one of the constants described in NSSearchPathDomainMask
. You should specify only one domain for your search and you may not specify the NSAllDomainsMask
constant for this parameter.
中文意思: 这个参数限定了文件搜索的区域..这个参数的值是NSSearchPathDomainMask描述的常量之一.你应该只指定一个域进行搜索以及不设置 NSAllDomainsMask这个参数
...
NSSearchPathDomainMask在文档描述如下:
struct NSSearchPathDomainMask : OptionSetType {
init(rawValue rawValue
: UInt)
static var UserDomainMask: NSSearchPathDomainMask { get }
static var LocalDomainMask: NSSearchPathDomainMask { get }
static var NetworkDomainMask: NSSearchPathDomainMask { get }
static var SystemDomainMask: NSSearchPathDomainMask { get }
static var AllDomainsMask: NSSearchPathDomainMask { get }
}
这个结构体中,最常用的参数是UserDomainMask.
url:The name of a directory inside of which you want to create a unique temporary directory for autosaving documents or some other use. This parameter is ignored unless the directory
parameter contains the value NSItemReplacementDirectory
and the domain
parameter contains the value NSUserDomainMask
. When creating a temporary directory, the shouldCreate
parameter is ignored and the directory is always created.
中文翻译:你想创建一个独特的目录用作自动保存文档或者其他用途,这个参数就是这个目录的名字.当在directory参数中设置为NSItemReplacementDirectory以及domain参数中设置为 NSUserDomainMask的时候
这个参数可以被忽略.当创建一个临时目录的时候,shouldCreate是被忽略的,并且会一直创建目录.
shouldCreate:Specify YES
if you want the directory to be created if it does not exist.
如果你想创建一个不存在的目录,你可以设置为YES
error:On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil
for this parameter if you do not want the error information.
当一个指针指向一个错误的对象的时候,他会输出. 如果一个错误出现,这个指针只想的错误对象包涵了错误信息,如果你不想获取到错误信息,你可以在这一项设置为nil.