---
title: duplicate()
framework: appkit
role: symbol
role_heading: Instance Method
path: appkit/nsdocument/duplicate()
---

# duplicate()

Creates a new document whose contents are the same as the receiver and returns an error object if unsuccessful.

## Declaration

```swift
func duplicate() throws -> NSDocument
```

## Return Value

Return Value The new document if duplication is successful; otherwise nil.

## Discussion

Discussion The new document returned doesn’t yet have a value to return from fileURL. The default implementation of this method first uses writeSafely(to:ofType:for:) to write the document’s current contents to a file located in the same directory that is used for the autosaved contents of untitled documents and with the same sort of name, then invokes [[NSDocumentController sharedDocumentController] duplicateDocumentWithContentsOfURL:newContentsURL copying:NO displayName:aDisplayName error:outError]. You can override this method to customize what is done during document duplication, but if your override does not invoke [NSDocumentController duplicateDocumentWithContentsOfURL:copying:displayName:error:] you must take care to do things that that method does, especially invoking [NSDocumentController addDocument:] and [NSFileCoordinator addFilePresenter:]. note: 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 in The Swift Programming Language and About Imported Cocoa Error Parameters.

## See Also

### Duplicating the Document

- [duplicate(_:)](appkit/nsdocument/duplicate(_:).md)
- [duplicate(withDelegate:didDuplicate:contextInfo:)](appkit/nsdocument/duplicate(withdelegate:didduplicate:contextinfo:).md)
