<!--
{
  "availability" : [
    "macOS: 10.5.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSDocument/fileNameExtension(forType:saveOperation:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDocument(im)fileNameExtensionForType:saveOperation:"
  },
  "title" : "fileNameExtension(forType:saveOperation:)"
}
-->

# fileNameExtension(forType:saveOperation:)

Returns a filename extension that can be appended to a base filename, for a specified file type and kind of save operation.

```
nonisolated func fileNameExtension(forType typeName: String, saveOperation: NSDocument.SaveOperationType) -> String?
```

## Parameters

`typeName`

The file type.

`saveOperation`

The kind of save operation.

## Return Value

The filename extension.

## Discussion

The default implementation of this method invokes [`preferredFilenameExtension(forType:)`](/documentation/AppKit/NSWorkspace/preferredFilenameExtension(forType:)) on the shared workspace object if the type is a UTI or, if it is not, for backward binary compatibility with OS X v10.4 and earlier, invokes [`fileExtensionsFromType:`](/documentation/AppKit/NSDocumentController/fileExtensionsFromType:) on the shared document controller and chooses the first filename extension in the returned array.

You can override this method to customize the appending of extensions to filenames by `NSDocument`. Starting in OS X v10.5, it is invoked from only two places in AppKit:

1. The [`autosave(withDelegate:didAutosave:contextInfo:)`](/documentation/AppKit/NSDocument/autosave(withDelegate:didAutosave:contextInfo:)) method uses this method when creating a new filename for the autosaved contents.
2. The [`handleSave(_:)`](/documentation/AppKit/NSDocument/handleSave(_:)) method uses this method when adding an extension to the filename specified by a script.

In all other cases, the name of any file being saved will have been fully specified by the user with the Save panel (whether they know it or not).

---

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)