'Focus TextField MacOS SwiftUI

I am trying to auto select a text field for a MacOS application. I added .focusable() but this does not change the result and the text field is still not auto selected and I need to click onto the text field to start typing.

...
TextField("text", text: $inputText)
   .focusable()
...


Solution 1:[1]

@FocusState is the property wrapper you need. Set the focus variable with your custom type. You can set this onAppear or based on any other conditions

  enum Focus : Hashable  
{
  case some             
}  
   
  @FocusState var focus : Focus?



TextField()
.focused($focus, equals: .some)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 arthas