---
title: "attach(device:completionHandler:)"
framework: virtualization
role: symbol
role_heading: Instance Method
path: "virtualization/vzusbcontroller/attach(device:completionhandler:)"
---

# attach(device:completionHandler:)

Attaches a USB device to the controller.

## Declaration

```swift
func attach(device: any VZUSBDevice, completionHandler: @escaping ((any Error)?) -> Void)
```

```swift
func attach(device: any VZUSBDevice) async throws
```

## Parameters

- `device`: The USB device to attach.
- `completionHandler`: A block the framework calls after the device attaches, or on an error. The error parameter that passes to the block is nil if attaching is successful. The framework calls the block on a VM’s queue.

## Discussion

Discussion If the device successfully attaches to the controller, it appears in the usbDevices property, the framework sets its usbController property to point to the attached USB controller, and the completion handler returns nil. If the device has a previous attachment to the current USB controller, or to another USB controller, the attach function fails with VZError.Code.deviceAlreadyAttached. If the controller can’t initialize the device correctly, the attach function fails with VZError.Code.deviceInitializationFailure. You need to call this method on the virtual machine’s queue.

## See Also

### Related Documentation

- [VZUSBDevice](virtualization/vzusbdevice.md)

### Attaching and detaching devices

- [detach(device:completionHandler:)](virtualization/vzusbcontroller/detach(device:completionhandler:).md)
