Instance Method

createDirectory(at:withIntermediateDirectories:attributes:)

Creates a directory with the given attributes at the specified URL.

Declaration

func createDirectory(at url: URL, withIntermediateDirectories createIntermediates: Bool, attributes: [FileAttributeKey : Any]? = nil) throws

Parameters

url

A file URL that specifies the directory to create. If you want to specify a relative path, you must set the current working directory before creating the corresponding NSURL object. This parameter must not be nil.

createIntermediates

If true, this method creates any nonexistent parent directories as part of creating the directory in url. If false, this method fails if any of the intermediate parent directories does not exist.

attributes

The file attributes for the new directory. You can set the owner and group numbers, file permissions, and modification date. If you specify nil for this parameter, the directory is created according to the umask(2) macOS Developer Tools Manual Page of the process. The Supporting Types section lists the global constants used as keys in the attributes dictionary. Some of the keys, such as hfsCreatorCode and hfsTypeCode, do not apply to directories.

Return Value

true if the directory was created, true if createIntermediates is set and the directory already exists, or false if an error occurred.

Discussion

If you specify nil for the attributes parameter, this method uses a default set of values for the owner, group, and permissions of any newly created directories in the path. Similarly, if you omit a specific attribute, the default value is used. The default values for newly created directories are as follows:

  • Permissions are set according to the umask of the current process. For more information, see umask.

  • The owner ID is set to the effective user ID of the process.

  • The group ID is set to that of the parent directory.

If you want to specify a relative path for url, you must set the current working directory before creating the corresponding NSURL object.

See Also

Creating and Deleting Items

func createFile(atPath: String, contents: Data?, attributes: [FileAttributeKey : Any]?) -> Bool

Creates a file with the specified content and attributes at the given location.

func removeItem(at: URL)

Removes the file or directory at the specified URL.

func removeItem(atPath: String)

Removes the file or directory at the specified path.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software