---
title: "lock(completionHandler:)"
framework: appkit
role: symbol
role_heading: Instance Method
path: "appkit/nsdocument/lock(completionhandler:)-161qv"
---

# lock(completionHandler:)

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

## Declaration

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

```swift
func lock() async throws
```

## Parameters

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

## Discussion

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.

## See Also

### Locking the Document

- [lock(_:)](appkit/nsdocument/lock(_:).md)
- [unlock(_:)](appkit/nsdocument/unlock(_:).md)
- [lock(completionHandler:)](appkit/nsdocument/lock(completionhandler:)-6zuhh.md)
- [unlock(completionHandler:)](appkit/nsdocument/unlock(completionhandler:)-8p8zd.md)
- [unlock(completionHandler:)](appkit/nsdocument/unlock(completionhandler:)-6m7rh.md)
- [isLocked](appkit/nsdocument/islocked.md)
