TechnologiesSwiftUI

DropOperation enum

iOSmacOStvOSwatchOSvisionOS✓ renders

Operation types that determine how a drag and drop session resolves when the

How it works

DropOperation is an enumeration that describes what happens to dragged content when the user releases it over a drop target. Each case names a distinct intent — copying the data, moving it, linking to it, forbidding the drop, or deferring the decision — so that both your code and the system's drag-and-drop affordances agree on the outcome. Reach for it whenever you handle a drop and need to record, reason about, or communicate which kind of transfer is taking place.

  1. Choose a case that names the transfer intent

    DropOperation models the outcome as a fixed set of cases — copy, move, forbidden, and related intents — rather than a free-form value, so the meaning is unambiguous wherever it travels. The example seeds its state with the .copy case, declaring that incoming items should be duplicated into the destination.

  2. Store the operation in observable state

    Because DropOperation is a plain value type, it slots directly into SwiftUI's data flow. The example holds it in @State private var operation: DropOperation = .copy, letting the current intent drive the view and update whenever a drop changes it.

  3. Set the operation as a drop is handled

    When a drop completes you decide which DropOperation applies and assign it. Inside the dropDestination(for:) closure, the example assigns operation = .copy as it receives the dropped items, recording that this drop was a copy before returning true to accept it.

  4. Read the case back to reflect the outcome

    Since each case is a discrete, comparable value, you can switch on it or render it to mirror the active intent in your UI. The example feeds the stored value into String(describing: operation) so the Label always shows the operation that the most recent drop produced.

Try it — Change operation = .copy inside the dropDestination(for:) closure to operation = .move and drop an item to watch the Label switch from reporting copy to move.

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

DropOperation.swift
struct DropOperationDemo: View {
    @State private var operation: DropOperation = .copy

    var body: some View {
        VStack(spacing: 16) {
            RoundedRectangle(cornerRadius: 12)
                .fill(.blue.opacity(0.15))
                .frame(height: 120)
                .overlay {
                    Text("Drop items here")
                        .foregroundStyle(.secondary)
                }
                .dropDestination(for: String.self) { items, location in
                    operation = .copy
                    return true
                }

            Label("Current operation: \(String(describing: operation))", systemImage: "doc.on.doc")
                .font(.headline)
        }
        .padding()
    }
}
Live preview
Drop items here Current operation: {String(describing: operation)}
Drop items here Current operation: {String(describing: operation)}
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →