Instance Method

string(forType:)

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

Declaration

func string(forType dataType: NSPasteboard.PasteboardType) -> String?

Parameters

dataType

The pasteboard data type to read.

Return Value

A concatenation of the strings for the specified type from all the items in the receiver that contain the type, or nil if none of the items contain strings of the specified type.

Discussion

This method invokes data(forType:) to obtain the string. If the string cannot be obtained, string(forType:) returns nil. See data(forType:) for a description of what will cause nil to be returned.

In macOS 10.6 and later, if the receiver contains multiple items that can provide string, RTF, or RTFD data, the text data from each item is returned as a combined result separated by newlines.

Special Considerations

It’s a good idea to check types or call availableType(from:) before invoking string(forType:). Although performing this check isn’t required, doing so can help you determine if a nil result from a reading method is due to something like a pasteboard timeout.

See Also

Reading Data

func readObjects(forClasses: [AnyClass], options: [NSPasteboard.ReadingOptionKey : Any]? = nil) -> [Any]?

Reads from the receiver objects that best match the specified array of classes.

struct NSPasteboard.ReadingOptionKey

These options can be used for both the readObjects(forClasses:options:) and canReadObject(forClasses:options:) methods, unless otherwise specified. The currently available options allow for customization of how URLS are read from the pasteboard.

var pasteboardItems: [NSPasteboardItem]?

An array that contains all the items held by the receiver or nil if there is an error retrieving pasteboard items.

func index(of: NSPasteboardItem) -> Int

Returns the index of the specified pasteboard item.

func data(forType: NSPasteboard.PasteboardType) -> Data?

Returns the data for the specified type from the first item in the receiver that contains the type.

func propertyList(forType: NSPasteboard.PasteboardType) -> Any?

Returns the property list for the specified type from the first item in the receiver that contains the type.