<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSDocument/fileAttributesToWrite(to:ofType:for:originalContentsURL:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDocument(im)fileAttributesToWriteToURL:ofType:forSaveOperation:originalContentsURL:error:"
  },
  "title" : "fileAttributesToWrite(to:ofType:for:originalContentsURL:)"
}
-->

# fileAttributesToWrite(to:ofType:for:originalContentsURL:)

Returns the attributes to write to the file or file package at the specified URL, and targeting the specified type of save operation.

```
nonisolated func fileAttributesToWrite(to url: URL, ofType typeName: String, for saveOperation: NSDocument.SaveOperationType, originalContentsURL absoluteOriginalContentsURL: URL?) throws -> [String : Any]
```

## Parameters

`url`

The location to which the document is being written.

`typeName`

The string that identifies the document type.

`saveOperation`

The type of save operation.

`absoluteOriginalContentsURL`

The location of the previously saved copy of the document (if not `nil`).

## Return Value

A dictionary containing the attributes to be written, or `nil` if unsuccessful.

## Discussion

Your subclass of `NSDocument` can override this method to control the attributes that are set during a save operation. An override of this method should return a copy of the dictionary returned by its superclass’s version of this method, with appropriate alterations.

The set of valid file attributes is a subset of those understood by the `NSFileManager` class. The default implementation of this method returns an empty dictionary for an `NSSaveOperation` or `NSAutosaveInPlaceOperation`, or a dictionary with an appropriate `NSFileExtensionHidden` entry for any other kind of save operation. You can override this method to customize the attributes that are written to document files.

For backward binary compatibility with OS X v10.5 and earlier, the default implementation of this method returns a dictionary with `NSFileHFSCode` and `NSFileHFSTypeCode` entries that have a value of 0 for `NSSaveOperation`, in apps linked against OS X v10.5 or earlier.

For backward binary compatibility with OS X v10.3 and earlier, the default implementation of this method instead invokes `[self fileAttributesToWriteToFile:[url path] ofType:typeName saveOperation:aSaveOperation]` if [`fileAttributesToWriteToFile:ofType:saveOperation:`](/documentation/AppKit/NSDocument/fileAttributesToWriteToFile:ofType:saveOperation:) is overridden and the URL uses the `file:` scheme. The save operation used in this case is never one of the autosaving ones: `NSSaveToOperation` is used instead.

The default implementation of [`writeSafely(to:ofType:for:)`](/documentation/AppKit/NSDocument/writeSafely(to:ofType:for:)) automatically copies important attributes like file permissions, creation date, and Finder information from the old on-disk version of a document to the new one during an `NSSaveOperation` or `NSAutosaveInPlaceOperation`. This method is meant to be used just for attributes that need to be written for the first time, for `NSSaveAsOperation` and `NSSaveToOperation`. The `url` and `absoluteOriginalContentsURL` parameters are passed in for completeness; NSDocument’s default implementation doesn’t need to use them.

> 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)