<!--
{
  "availability" : [
    "macCatalyst: -",
    "macOS: 10.0.0 - 10.4.0"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSDocument/writeToFile:ofType:",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDocument(im)writeToFile:ofType:"
  },
  "title" : "writeToFile:ofType:"
}
-->

# writeToFile:ofType:

Writes document data to a file.

```
- (BOOL) writeToFile:(NSString *) fileName ofType:(NSString *) type;
```

## Discussion

Writes document data of type `docType` to the file `fileName`, returning whether the operation was successful. This method invokes [`dataRepresentationOfType:`](/documentation/AppKit/NSDocument/dataRepresentationOfType:) and is indirectly invoked whenever the document file is saved. It uses the `NSData` method <doc://com.apple.documentation/documentation/Foundation/NSData/write(toFile:atomically:)> to write to the file.

This method is one of the location-based primitives. Subclasses can override this method instead of overriding `dataRepresentationOfType:` to write document data to the file system as an `NSData` object after creating that object from internal data structures. Subclasses that handle file packages such as RTFD or that treat locations of files as anything other than paths should override this method. Override implementations of this method should ensure that they filter document data appropriately using `NSPasteboard` filtering services.

See [`Document Saving Behavior`](/documentation/AppKit/NSDocument#Document-Saving-Behavior) for additional information about saving documents.

---

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)