---
title: "writeDatagram(_:completionHandler:)"
framework: networkextension
role: symbol
role_heading: Instance Method
path: "networkextension/nwudpsession/writedatagram(_:completionhandler:)"
---

# writeDatagram(_:completionHandler:)

Write a single datagram.

## Declaration

```swift
func writeDatagram(_ datagram: Data, completionHandler: @escaping ((any Error)?) -> Void)
```

## Parameters

- `datagram`: An doc://com.apple.documentation/documentation/Foundation/NSData object containing the datagram to write. To improve performance, this should be an immutable data object.
- `completionHandler`: A handler called when the write request has either succeeded or failed.

## Discussion

Discussion Callers should wait until the completionHandler is executed before issuing another write.

## See Also

### Transferring data

- [setReadHandler(_:maxDatagrams:)](networkextension/nwudpsession/setreadhandler(_:maxdatagrams:).md)
- [writeMultipleDatagrams(_:completionHandler:)](networkextension/nwudpsession/writemultipledatagrams(_:completionhandler:).md)
- [maximumDatagramLength](networkextension/nwudpsession/maximumdatagramlength.md)
