<!--
{
  "availability" : [
    "macOS: 10.8.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSDocument/lock(completionHandler:)-161qv",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDocument(im)lockWithCompletionHandler:"
  },
  "title" : "lock(completionHandler:)"
}
-->

# lock(completionHandler:)

Prevents the user from making changes to the document’s file.

```
func lock(completionHandler: (((any Error)?) -> Void)? = nil)
```

## Parameters

`completionHandler`

The completion handler block object passed in to be invoked after locking is completed, regardless of success or failure of locking.

## Discussion

This method first locks the file at `[self fileURL]` and then invokes the given block. The default locking implementation is to enable the “user immutable” flag on the file.

---

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)