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.
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.copycase, declaring that incoming items should be duplicated into the destination.Store the operation in observable state
Because
DropOperationis 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.Set the operation as a drop is handled
When a drop completes you decide which
DropOperationapplies and assign it. Inside thedropDestination(for:)closure, the example assignsoperation = .copyas it receives the droppeditems, recording that this drop was a copy before returningtrueto accept it.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 theLabelalways shows the operation that the most recent drop produced.
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.
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()
}
}