---
title: "write(contents:to:at:replyHandler:)"
framework: fskit
role: symbol
role_heading: Instance Method
path: "fskit/fsvolume/readwritehandler/write(contents:to:at:replyhandler:)"
---

# write(contents:to:at:replyHandler:)

Writes contents to the given file item.

## Declaration

```swift
func write(contents: Data, to item: FSItem, at offset: off_t, replyHandler reply: @escaping @Sendable (FSWriteFileResult?, (any Error)?) -> Void)
```

```swift
func write(contents: Data, to item: FSItem, at offset: off_t) async throws -> FSWriteFileResult
```

## Parameters

- `contents`: A buffer containing the data to write to the file.
- `item`: The item to which to write. FSKit guarantees this item will be of type doc://FSKit/documentation/FSKit/FSItem/ItemType/file.
- `offset`: The offset in the file from which to start writing.
- `reply`: A block or closure to indicate success or failure. If writing succeeds, pass an instance of doc://FSKit/documentation/FSKit/FSWriteFileResult containing the number of bytes written, the updated doc://FSKit/documentation/FSKit/FSItem/Attributes of the file, and the volume’s updated free space, along with a nil error. If writing fails, pass the relevant error as the second parameter; FSKit ignores the doc://FSKit/documentation/FSKit/FSWriteFileResult instance in this case. For an async Swift implementation, there’s no reply handler; simply return the result instance or throw an error.

## Discussion

Discussion FSKit expects this routine to allocate space in the file system to extend the file as necessary. If the volume experiences an out-of-space condition, reply with an error of domain NSPOSIXErrorDomain and code ENOSPC.

## See Also

### Reading and writing

- [read(from:at:length:into:replyHandler:)](fskit/fsvolume/readwritehandler/read(from:at:length:into:replyhandler:).md)
- [FSMutableFileDataBuffer](fskit/fsmutablefiledatabuffer.md)
- [FSReadFileResult](fskit/fsreadfileresult.md)
- [FSWriteFileResult](fskit/fswritefileresult.md)
