<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.5.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/FileManager/setAttributes(_:ofItemAtPath:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSFileManager(im)setAttributes:ofItemAtPath:error:"
  },
  "title" : "setAttributes(_:ofItemAtPath:)"
}
-->

# setAttributes(_:ofItemAtPath:)

Sets the attributes of the specified file or directory.

```
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`](/documentation/Foundation/FileAttributeKey/busy), [`creationDate`](/documentation/Foundation/FileAttributeKey/creationDate), [`extensionHidden`](/documentation/Foundation/FileAttributeKey/extensionHidden), [`groupOwnerAccountID`](/documentation/Foundation/FileAttributeKey/groupOwnerAccountID), [`groupOwnerAccountName`](/documentation/Foundation/FileAttributeKey/groupOwnerAccountName), [`hfsCreatorCode`](/documentation/Foundation/FileAttributeKey/hfsCreatorCode), [`hfsTypeCode`](/documentation/Foundation/FileAttributeKey/hfsTypeCode), [`immutable`](/documentation/Foundation/FileAttributeKey/immutable), [`modificationDate`](/documentation/Foundation/FileAttributeKey/modificationDate), [`ownerAccountID`](/documentation/Foundation/FileAttributeKey/ownerAccountID), [`ownerAccountName`](/documentation/Foundation/FileAttributeKey/ownerAccountName), [`posixPermissions`](/documentation/Foundation/FileAttributeKey/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.

## 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, the system traverses it.

You must initialize the [`posixPermissions`](/documentation/Foundation/FileAttributeKey/posixPermissions) value with the code representing the POSIX file-permissions bit pattern. The system sets [`hfsCreatorCode`](/documentation/Foundation/FileAttributeKey/hfsCreatorCode) and [`hfsTypeCode`](/documentation/Foundation/FileAttributeKey/hfsTypeCode) only when `path` specifies a file.

> Handling Errors in Swift:
> In Swift, this method returns `Void` and is marked with the `throws` keyword to indicate that it throws an error in cases of failure.
> 
> You call this method in a `try` expression and handle any errors in the `catch` clauses of a `do` statement, as described in [Error Handling](https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html) in [The Swift Programming Language](https://docs.swift.org/swift-book/) and `About Imported Cocoa Error Parameters`.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)