Contents

FocusedValueKey

A protocol for identifier types used when publishing and observing focused values.

Declaration

protocol FocusedValueKey

Overview

Unlike EnvironmentKey, FocusedValueKey has no default value requirement, because the default value for a key is always nil.

Use the Entry macro to create custom focused values by extending FocusedValues with new properties:

extension FocusedValues {
    @Entry var selectedItem: Item?
}

Alternatively it is possible to create a focused value key by manually creating a type that conforms to this protocol:

struct SelectedItemKey: FocusedValueKey {
    typealias Value = Item
}

Then extend FocusedValues to add a computed property for your key:

extension FocusedValues {
    var selectedItem: Item? {
        get { self[SelectedItemKey.self] }
        set { self[SelectedItemKey.self] = newValue }
    }
}

Topics

Specifying the value type

See Also

Managing focus state