How to use `.focusedSceneObject()` with @Observable?

According to docs, .focusedObject() usage should be moved to .focusedValue() when migrating to @Observable, but there is no .focusedSceneValue() overload that accepts Observable like with .focusedValue(). So how are we supposed migrate .focusedSceneObject() to @Observable?

The migration from .focusedSceneObject() to @Observable in SwiftUI can be achieved by using a @State property to store the focused value and then binding the focused property of the view to this @State property. Here's how you can migrate from .focusedSceneObject() to @Observable:

  1. Define an @Observable Property:

    Create an @Observable property that will store the focused value. This property can be of any appropriate data type, such as a String or a custom struct. For example:

    @Observable var focusedValue: String = ""
    
  2. Bind focused to the @Observable Property:

    Instead of using .focusedSceneObject(), you can bind the focused property of a view to your @Observable property using the $ syntax. This allows you to set and get the focused value.

    For example, if you were using .focusedSceneObject() like this:

    .focusedSceneObject(\.focusedValue)
    

    You can migrate it to @Observable like this:

    .focused($focusedValue)
    

    This binding connects the view's focus state to the focusedValue property.

  3. Update Usage of the Focused Value:

    Wherever you were using .focusedSceneObject(\.focusedValue), you can now use the focusedValue property directly.

    For example, if you had a Text view displaying the focused value:

    Text("Focused Value: \(focusedValue)")
    

    This code will still work with the focusedValue property defined in step 1.

By following these steps, you can migrate from .focusedSceneObject() to @Observable while maintaining the functionality of managing the focused value in your SwiftUI view.

How to use `.focusedSceneObject()` with @Observable?
 
 
Q