Declaration
compose().copyable<T>(payload: T[])nonisolated func copyable<T>(
_ payload: @autoclosure @escaping () -> [T]
) -> some View
where T : TransferableParameters
payload*T[]A closure that returns an array of items to copy to the Clipboard when someone issues a Copy command. The items in the array must conform to the doc://com.apple.documentation/documentation/coretransferable/transferable protocol.
Return value
A view that adds one or more items to the Clipboard in response to a Copy command.
Discussion
Use this modifier to specify one or more items that the system copies to the Clipboard when someone issues a Copy command while the modified view has focus. People issue a Copy command by choosing Edit > Copy from the app's menu, or by using the Command-C keyboard shortcut. The system enables the Copy command for your app when it detects copyable content.
For example, the following code enables people to copy all of the
strings that they select in a List:
struct CopyableExample: View {
let strings = ["Alpha", "Beta", "Gamma"]
@State private var selection: Set<String> = []
var body: some View {
List(strings, id: \.self, selection: $selection) {
Text($0)
}
.copyable(Array(selection))
}
}The command copies each item's representation as specified by the
item's conformance to the
doc://com.apple.documentation/documentation/coretransferable/transferable
protocol. The above example records selection using each list item's
corresponding string, and strings conform to the Transferable protocol
by default. For more complex cases, you might need to take additional
steps.
For example, the following code displays colors composed from a list
of Item instances that contain both a color and its name, as well as
an identifier. The list manages selection using item identifiers:
struct Item: Identifiable, Transferable {
let color: Color
let name: String
let id = UUID()
static var transferRepresentation: some TransferRepresentation {
ProxyRepresentation(exporting: \.name)
}
}
struct CopyableIDExample: View {
let items: [Item] = [
Item(color: .red, name: "red"),
Item(color: .green, name: "green"),
Item(color: .blue, name: "blue")
]
@State private var selection: Set<Item.ID> = []
var body: some View {
List(items, selection: $selection) { item in
item.color
}
.copyable(items.filter { selection.contains($0.id) })
}
}This example does two things that the previous example didn't have to:
- It converts the list of selected item identifiers into a list of selected items for use with the copyable modifier.
- It ensures that the copyable items conform to the
doc://com.apple.documentation/documentation/coretransferable/transferable
protocol, using the item's
nameproperty as the representation.
When someone copies the first item in the list, which appears in the interface as a red rectangle, and then pastes into a text editor, they get the string "red".
context menu item --- rather than using the system Copy command, update the Clipboard directly using an doc://com.apple.documentation/documentation/appkit/nspasteboard or a doc://com.apple.documentation/documentation/uikit/uipasteboard instance.
See also
More in Drag and drop.
- cuttableSpecifies an action that moves items to the Clipboard in response to the system's Cut command.
- documentLaunchSubtitleSets the subtitle displayed beneath the title on the document launch card, using a localized string resource.
- documentLaunchTitleSets the title displayed on the document launch card, using a localized string resource.
- draggableActivates this view as the source of a drag and drop operation.
- dropConfigurationConfigures a drop session.
- dropDestinationDefines the destination of a drag and drop operation that provides a drop operation proposal and handles the dropped content with a closure that you specify.
- fileExporterPresents a system dialog allowing the user to export a Transferable item to a file on disk.
- fileExporterFilenameLabelOn macOS, configures the fileExporter with a label for the file name field.
- fileImporterPresents a system dialog for allowing the user to import multiple files.
- fileMoverPresents a system dialog for allowing the user to move an existing file to a new location.
- itemProviderProvides a closure that vends the drag representation to be used for a particular data element.
- pasteDestinationSpecifies an action that adds validated items to a view in response to the system's Paste command.