<!--
{
  "availability" : [
    "macOS: 10.7.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSDocument/duplicate(withDelegate:didDuplicate:contextInfo:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDocument(im)duplicateDocumentWithDelegate:didDuplicateSelector:contextInfo:"
  },
  "title" : "duplicate(withDelegate:didDuplicate:contextInfo:)"
}
-->

# duplicate(withDelegate:didDuplicate:contextInfo:)

Creates a new document whose contents are the same as the current document.

```
func duplicate(withDelegate delegate: Any?, didDuplicate didDuplicateSelector: Selector?, contextInfo: UnsafeMutableRawPointer?)
```

## Parameters

`delegate`

The delegate to which the selector message is sent.

`didDuplicateSelector`

The selector of the message sent to the delegate.

`contextInfo`

Object passed with the callback to provide any additional context information.

## Discussion

The new document that is created doesn’t yet have a value to return from [`fileURL`](/documentation/AppKit/NSDocument/fileURL). When duplicating is completed, regardless of success or failure, or whether the operation is rejected by the user, this method sends the message indicated by `didDuplicateSelector` to the delegate, with `contextInfo` as the last argument. The method selected by `didDuplicateSelector` must have the same signature as:

```objc
- (void)document:(NSDocument *)document didDuplicate:(BOOL)didDuplicate contextInfo:(void *)contextInfo;
```

The default implementation of this method first makes sure that any editor registered using the Cocoa Bindings `NSEditorRegistration` informal protocol has committed its changes, then checks to see if there are recent changes that might have been inadvertent and, if so, presents a panel giving the user the choice of canceling, duplicating, or duplicating then discarding recent changes. Unless the user cancels duplicating, or if no panel was presented, it then invokes [`duplicate()`](/documentation/AppKit/NSDocument/duplicate()). If the user chose duplicating and discarding, it also discards recent changes after duplicating.

---

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)