Instance Method


Locates and optionally creates the specified common directory in a domain.


func url(for directory: FileManager.SearchPathDirectory, in domain: FileManager.SearchPathDomainMask, appropriateFor url: URL?, create shouldCreate: Bool) throws -> URL



The search path directory. The supported values are described in FileManager.SearchPathDirectory.


The file system domain to search. The value for this parameter is one of the constants described in FileManager.SearchPathDomainMask. You should specify only one domain for your search and you may not specify the allDomainsMask constant for this parameter.


The file URL used to determine the location of the returned URL. Only the volume of this parameter is used.

This parameter is ignored unless the directory parameter contains the value FileManager.SearchPathDirectory.itemReplacementDirectory and the domain parameter contains the value userDomainMask.


Whether to create the directory if it does not already exist.

When creating a temporary directory, this parameter is ignored and the directory is always created.

Return Value

The NSURL for the requested directory.


You typically use this method to locate one of the standard system directories, such as the Documents, Application Support or Caches directories. After locating (or creating) the desired directory, this method returns the URL for that directory. If more than one appropriate directory exists in the specified domain, this method returns only the first one it finds.

You can use this method to create a new temporary directory. To do so, specify FileManager.SearchPathDirectory.itemReplacementDirectory for the directory parameter, userDomainMask for the domain parameter, and a URL for the url parameter which determines the volume of the returned URL.

For example, the following code results in a new temporary directory with a path in the form of /private/var/folders/d0/h37cw8ns3h1bfr_2gnwq2yyc0000gn/T/TemporaryItems/Untitled/:

let desktop = URL(fileURLWithPath: "/Users/jappleseed/Desktop/")

do {
    let temporaryDirectory = try FileManager.default.url(
        for: .itemReplacementDirectory,
        in: .userDomainMask,
        appropriateFor: desktop,
        create: true
} catch {
    // Handle the error.

See Also

Locating System Directories

func urls(for: FileManager.SearchPathDirectory, in: FileManager.SearchPathDomainMask) -> [URL]

Returns an array of URLs for the specified common directory in the requested domains.

func NSOpenStepRootDirectory() -> String

Returns the root directory of the user’s system.