---
title: "encode(_:)"
framework: browserenginekit
role: symbol
role_heading: Instance Method
path: "browserenginekit/layerhierarchyhandle/encode(_:)"
---

# encode(_:)

Serializes the hierarchy handle into a Mach port reference and accompanying data.

## Declaration

```swift
func encode(_ block: (mach_port_t, Data) -> Void)
```

## Parameters

- `block`: A closure that receives a Mach port and serialized data as arguments.

## Discussion

Discussion This method invokes the supplied closure with a mach_port_t and Data object that together represent the serialized layer hierarchy handle. The port serves as a reference to the layer hierarchy that you can share with another process. Use the port and data together to reconstruct the handle using init(port:data:). handle.encode { port, data in     guard port != MACH_PORT_NULL else {         // The handle is already invalidated.         return     }          // Send the port and data to the rendering process.     sendToRenderingExtension(port: port, data: data)          // Dispose of the port after sending.     mach_port_deallocate(mach_task_self(), port) } important: Dispose of the Mach port in the supplied closure after sending it to the other process. The port is MACH_PORT_NULL if the handle has already been invalidated (see invalidate()). Each call to this method creates a new port reference and unique serialized data. Decode each port and data pair only once using init(port:data:).

## See Also

### Sharing a layer hierarchy handle using Mach

- [init(port:data:)](browserenginekit/layerhierarchyhandle/init(port:data:).md)
