<!--
{
  "availability" : [
    "iOS: 5.0.0 -",
    "iPadOS: 5.0.0 -",
    "macCatalyst: 13.1.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/UIDocument/fileAttributesToWrite(to:for:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(cs)UIDocument(im)fileAttributesToWriteToURL:forSaveOperation:error:"
  },
  "title" : "fileAttributesToWrite(to:for:)"
}
-->

# fileAttributesToWrite(to:for:)

Returns a dictionary of file attributes to associate with the document file when writing or updating it.

```
func fileAttributesToWrite(to url: URL, for saveOperation: UIDocument.SaveOperation) throws -> [AnyHashable : Any]
```

## Parameters

`url`

A file URL locating the document in the application sandbox.

`saveOperation`

A constant that indicates whether the document file is being written the first time or whether it’s being overwritten. See [`UIDocument.SaveOperation`](/documentation/UIKit/UIDocument/SaveOperation) for details.

## Return Value

A dictionary of file attributes — for example, level of file protection and creation date. See <doc://com.apple.documentation/documentation/Foundation/FileManager> for more information about file attributes.

## Discussion

The attributes are associated with a specific file type and save operation. You can override this method to return a dictionary of file attributes that are different than the default file attribute, which for new files is <doc://com.apple.documentation/documentation/Foundation/FileAttributeKey/extensionHidden>.

The [`save(to:for:completionHandler:)`](/documentation/UIKit/UIDocument/save(to:for:completionHandler:)) calls this method before executing asynchronous writing. It passes the dictionary into [`writeContents(_:andAttributes:safelyTo:for:)`](/documentation/UIKit/UIDocument/writeContents(_:andAttributes:safelyTo:for:)) when it calls that method to write the document file.

> Handling Errors in Swift:
> In Swift, this method is marked with the `throws` keyword to indicate that it throws an error in cases of failure.
> 
> When overriding this method, use the `throw` statement to throw an `NSError`, 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)