<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSPasteboard/setString(_:forType:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSPasteboard(im)setString:forType:"
  },
  "title" : "setString(_:forType:)"
}
-->

# setString(_:forType:)

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

```
func setString(_ string: String, forType dataType: NSPasteboard.PasteboardType) -> Bool
```

## Parameters

`string`

The string to write to the pasteboard.

`dataType`

The type of string data. The type must have been declared by a previous [`declareTypes(_:owner:)`](/documentation/AppKit/NSPasteboard/declareTypes(_:owner:)) message.

## Return Value

<doc://com.apple.documentation/documentation/Swift/true> if the data was written successfully, otherwise <doc://com.apple.documentation/documentation/Swift/false> if ownership of the pasteboard has changed. Any other error raises an `NSPasteboardCommunicationException`.

## Discussion

This method invokes [`setData(_:forType:)`](/documentation/AppKit/NSPasteboard/setData(_:forType:)) to perform the write.

## See Also

[`-  stringForType:`](/documentation/AppKit/NSPasteboard/string(forType:))

Returns a concatenation of the strings for the specified type from all the items in the receiver that contain the type.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)