---
title: "setData(_:forType:)"
framework: appkit
role: symbol
role_heading: Instance Method
path: "appkit/nspasteboard/setdata(_:fortype:)"
---

# setData(_:forType:)

Sets the data as the representation for the specified type for the first item on the receiver.

## Declaration

```swift
func setData(_ data: Data?, forType dataType: NSPasteboard.PasteboardType) -> Bool
```

## Parameters

- `data`: The data to write to the pasteboard.
- `dataType`: The type of data in the data parameter. The type must have been declared by a previous doc://com.apple.appkit/documentation/AppKit/NSPasteboard/declareTypes(_:owner:) message.

## Return Value

Return Value true if the data was written successfully, otherwise false if ownership of the pasteboard has changed. Any other error raises an NSPasteboardCommunicationException.

## See Also

### Related Documentation

- [data(forType:)](appkit/nspasteboard/data(fortype:).md)

### Writing data

- [clearContents()](appkit/nspasteboard/clearcontents().md)
- [writeObjects(_:)](appkit/nspasteboard/writeobjects(_:).md)
- [setPropertyList(_:forType:)](appkit/nspasteboard/setpropertylist(_:fortype:).md)
- [setString(_:forType:)](appkit/nspasteboard/setstring(_:fortype:).md)
- [NSPasteboard.PasteboardType](appkit/nspasteboard/pasteboardtype.md)
