<!--
{
  "availability" : [
    "iOS: 27.0.0 -",
    "iPadOS: 27.0.0 -",
    "macCatalyst: 27.0.0 -",
    "macOS: 26.0.0 -",
    "visionOS: 27.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/dragContainerSelection(_:containerNamespace:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE22dragContainerSelection_18containerNamespaceQrSayqd__GyXA_AA0H0V2IDVSgtSHRd__s8SendableRd__lF"
  },
  "title" : "dragContainerSelection(_:containerNamespace:)"
}
-->

# dragContainerSelection(_:containerNamespace:)

Provides multiple item selection support for drag containers.

```
nonisolated func dragContainerSelection<ItemID>(_ selection: @autoclosure @escaping () -> Array<ItemID>, containerNamespace: Namespace.ID? = nil) -> some View where ItemID : Hashable, ItemID : Sendable
```

## Parameters

`selection`

A closure that provides identifiers of selected items.

`containerNamespace`

An optional namespace of the drag container.

## Discussion

A drag container finds the nearest enclosing `dragContainerSelection(_:containerNamespace:)`
with the same item identifier type and same namespace, if specified.
Drag container uses the provided selected item identifiers to determine what
the drag payload should be.

If the dragged view is associated with a selected identifier, the payload should contain
all the selected items. If the dragged view is not selected, the payload should
not contain the whole selection, just the dragged item.
With `dragContainerSelection(_:containerNamespace:)`, you get fine-grained control
over what items are included in the drag payload.

```
 struct FruitContainer: View {
      @State private var fruits: [Fruit]
      @State private var selection: [Fruit.ID]

      var body: some View {
          VStack {
              ForEach(fruits) { fruit in
                  FruitView(fruit)
                      .draggable(containerItemID: fruit.id)
               }
           }
          .dragContainer(for: Fruit.self) { ids in
              fruits(with: ids)
          }
          .dragContainerSelection(selection)
      }

    func fruits(with ids: [Fruit.ID]) -> [Fruit] { ... }

    struct Fruit: Transferable, Identifiable {
        let id: String
        ...
    }

    struct FruitView: View {
        init(_ fruit: Fruit) { ... }
    }
}
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)