Instance Method

setAttributes(_:ofItemAtPath:)

Sets the attributes of the specified file or directory.

Declaration

func setAttributes(_ attributes: [FileAttributeKey : Any], ofItemAtPath path: String) throws

Parameters

attributes

A dictionary containing as keys the attributes to set for path and as values the corresponding value for the attribute. You can set the following attributes: busy, creationDate, extensionHidden, groupOwnerAccountID, groupOwnerAccountName, hfsCreatorCode, hfsTypeCode, immutable, modificationDate, ownerAccountID, ownerAccountName, posixPermissions. You can change single attributes or any combination of attributes; you need not specify keys for all attributes.

path

The path of a file or directory.

Return Value

true if all changes succeed. If any change fails, returns false, but it is undefined whether any changes actually occurred.

Discussion

As in the POSIX standard, the app either must own the file or directory or must be running as superuser for attribute changes to take effect. The method attempts to make all changes specified in attributes and ignores any rejection of an attempted modification. If the last component of the path is a symbolic link it is traversed.

The posixPermissions value must be initialized with the code representing the POSIX file-permissions bit pattern. hfsCreatorCode and hfsTypeCode will only be heeded when path specifies a file.

See Also

Getting and Setting Attributes

func componentsToDisplay(forPath: String) -> [String]?

Returns an array of strings representing the user-visible components of a given path.

func displayName(atPath: String) -> String

Returns the display name of the file or directory at a specified path.

func attributesOfItem(atPath: String) -> [FileAttributeKey : Any]

Returns the attributes of the item at a given path.

func attributesOfFileSystem(forPath: String) -> [FileAttributeKey : Any]

Returns a dictionary that describes the attributes of the mounted file system on which a given path resides.

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