<!--
{
  "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/writeContents(_:to:for:originalContentsURL:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(cs)UIDocument(im)writeContents:toURL:forSaveOperation:originalContentsURL:error:"
  },
  "title" : "writeContents(_:to:for:originalContentsURL:)"
}
-->

# writeContents(_:to:for:originalContentsURL:)

Writes the document data to disk at the sandbox location indicated by a file URL.

```
func writeContents(_ contents: Any, to url: URL, for saveOperation: UIDocument.SaveOperation, originalContentsURL: URL?) throws
```

## Parameters

`contents`

The document data to write to disk. Typically, the data is encapsulated by an <doc://com.apple.documentation/documentation/Foundation/NSData> object (if a flat file) or an <doc://com.apple.documentation/documentation/Foundation/FileWrapper> object (if a file package).

    If the object encapsulating the document data is of some other type, you should override this method or [`writeContents(_:andAttributes:safelyTo:for:)`](/documentation/UIKit/UIDocument/writeContents(_:andAttributes:safelyTo:for:))     to perform the actual writing of the data.

`url`

A file URL specifying the location of the document file in the application sandbox.

`saveOperation`

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

`originalContentsURL`

A file URL specifying the previous location of the document file (if not `nil`).

## Discussion

This method is called by the [`writeContents(_:andAttributes:safelyTo:for:)`](/documentation/UIKit/UIDocument/writeContents(_:andAttributes:safelyTo:for:)) to write the actual file data. It is passed the contents object returned from your [`contents(forType:)`](/documentation/UIKit/UIDocument/contents(forType:)) implementation. The default implementation of this method supports <doc://com.apple.documentation/documentation/Foundation/NSData> or <doc://com.apple.documentation/documentation/Foundation/FileWrapper> contents by asking the contents object to save itself to the corresponding URL.

If you override this method, you may choose to return a different type of data from [`contents(forType:)`](/documentation/UIKit/UIDocument/contents(forType:)) or you may choose to not override [`contents(forType:)`](/documentation/UIKit/UIDocument/contents(forType:)) and generate the writable data directly within this method. If you override this method, you should not invoke the superclass implementation.

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