<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/tag(_:includeOptional:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE3tag_15includeOptionalQrqd___SbtSHRd__lF"
  },
  "title" : "tag(_:includeOptional:)"
}
-->

# tag(_:includeOptional:)

Sets the unique tag value of this view.

```
@export(implementation) nonisolated func tag<V>(_ tag: V, includeOptional: Bool = true) -> some View where V : Hashable
```

## Parameters

`tag`

A <doc://com.apple.documentation/documentation/Swift/Hashable>
value to use as the view’s tag.

`includeOptional`

If the tag value for `Optional<V>` should
also be set.

## Return Value

A view with the specified tag set.

## Discussion

Use this modifier to differentiate among certain selectable views,
like the possible values of a [`Picker`](/documentation/SwiftUI/Picker) or the tabs of a [`TabView`](/documentation/SwiftUI/TabView).
Tag values can be of any type that conforms to the
<doc://com.apple.documentation/documentation/Swift/Hashable> protocol.

This modifier will write the tag value for the type `V`, as well as
`Optional<V>` if `includeOptional` is enabled. Containers checking for
tags of either type will see the value as set.

In the example below, the [`ForEach`](/documentation/SwiftUI/ForEach) loop in the [`Picker`](/documentation/SwiftUI/Picker) view
builder iterates over the `Flavor` enumeration. It extracts the string
value of each enumeration element for use in constructing the row
label, and uses the enumeration value as input to the `tag(_:)`
modifier.

```
struct FlavorPicker: View {
    enum Flavor: String, CaseIterable, Identifiable {
        case chocolate, vanilla, strawberry
        var id: Self { self }
    }

    @State private var selectedFlavor: Flavor? = nil

    var body: some View {
        Picker("Flavor", selection: $selectedFlavor) {
            ForEach(Flavor.allCases) { flavor in
                Text(flavor.rawValue)
                    .tag(flavor)
            }
        }
    }
}
```

The selection type of the [`Picker`](/documentation/SwiftUI/Picker) is an `Optional<Flavor>` and so it
will look for tags on its contents of `Optional<Flavor>` type. Since the
tag modifier defaults to having `includeOptional` enabled, even though
the tag for each option is a non-optional `Flavor`, the tag modifier
writes values for both the non-optional, and optional versions of the
value, allowing the contents to be selectable by the [`Picker`](/documentation/SwiftUI/Picker).

A [`ForEach`](/documentation/SwiftUI/ForEach) automatically applies a default tag to each enumerated
view using the `id` parameter of the corresponding element. If
the element’s `id` parameter and the picker’s `selection` input
have exactly the same type, or the same type but optional, you can omit
the explicit tag modifier.

To see examples that don’t require an explicit tag, see [`Picker`](/documentation/SwiftUI/Picker).

---

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)