pasteDestination
Specifies an action that adds validated items to a view in response to the system's Paste command.
Declaration
compose().pasteDestination<T>({
action: (arg0: T[]) => void,
validator?: { returns: T[]; perform?: (arg0: T[]) => void; },
})nonisolated func pasteDestination<T>(
for payloadType: T.Type = T.self,
action: @escaping ([T]) -> Void,
validator: @escaping ([T]) -> [T] = { $0 }
) -> some View
where T : TransferableParameters
options.action*(arg0: T[]) => voidThe action to perform when someone uses the system's Paste command to paste one or more items of the payload type. The closure takes one parameter, which is the array of items to paste.
options.validator{ returns: T[]; perform?: (arg0: T[]) => void; }A closure that you implement to validate the data to paste. SwiftUI calls this before it calls the
actionclosure, and passes in an array of items to validate. Inspect the items, and return an array that includes only those from the input array that you consider valid. The array that you return from this closure becomes the input to theactionclosure. If you return an empty array, SwiftUI doesn't call theactionclosure.
Return value
A view that people can paste into using the system Paste command.
Discussion
Use this modifier to paste one or more items into a view from the Clipboard when someone issues a Paste command. People issue a Paste command by choosing Edit > Paste from the app's menu, or by using the Command-V keyboard shortcut. The system enables the Paste command for your app when the view in focus provides a paste destination.
For example, the following code enables people to paste bird names into a list:
struct PasteDestinationExample: View {
@State private var birds: [String] = []
@State private var selection: Set<String> = []
let knownBirds = ["owl", "parrot", "swift",
"sparrow", "robin", "bluebird"]
var body: some View {
List(birds, id: \.self, selection: $selection) {
Text($0)
}
.pasteDestination(for: String.self) { values in
birds.append(contentsOf: values)
} validator: { values in
values.filter { knownBirds.contains($0) }
}
}
}The paste destination handles only pasted content with a type that
matches the payloadType that you specify. The list in the above
example only accepts strings.
Use the validator closure to restrict the pasted content to items that
make sense in the context of the view. The above example allows people
to paste only strings that match one of a known list of bird names
because the list is meant to contain only birds. You can omit the final
closure if you don't need to perform any validation.
context menu item --- rather than using the system Paste command, access 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.
- copyableSpecifies a list of items to copy in response to the system's Copy command.
- 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.