After spending an embarrassing amount of hours trying to get TextField to do what I needed on macOS I decided to try NSViewRepresentable + NSTextField.
I was fully prepared to spend the night on this… Yet I got it done in 30m 😮Â
Lesson learned! Going forward, when a SwiftUI object is not mature enough I will not try to hack it with modifiers… Instead,  I will go straight to AppKit or UIKit. I know I’m loosing portability – which is why I spend so much time trying to do it all in SwiftUI – but, especially on macOS, SwiftUI sometimes is just not yet there.Â
So, about linking to AppKit or UIKit… Online there’s a lot of, shall we say, over-engineered examples? Frankly, I believe it puts off a lot of people -including myself-, even though its actually very easy.Â
Below is the leanest approach I found to link to any AppKit/UIKit class and their respective delegate. For this example I’m linking to NSTextField, but you could do it just as easily with any class.
Here’s how you call it from SwiftUI once implemented:
OSXTextField(textOfWhatToDisplay: someContent)
And here is the wrapper code for NSTextField, all self contained within a Struct:
struct OSXTextField: NSViewRepresentable {
Â
  var textOfWhatToDisplay: String
  Â
  func makeNSView(context:Context) -> NSTextField{
    let osTextView = NSTextField(string: textOfWhatToDisplay)
    osTextView.delegate = context.coordinator   Â
//Â Â Â osTextView.isBordered = false //example of calling NSTextFieldÂ
//Â Â Â osTextView.drawsBackground = true //
    return osTextView
  }
  Â
  func updateNSView(_ nsView: NSTextField, context: Context) {
Â
  }
  Â
  func makeCoordinator() -> Coordinator { //Called automatically by SwiftUI if implemented, used to trigger delegate
    Coordinator(parent: self)
  }
  Â
  class Coordinator: NSObject, NSTextFieldDelegate {
    Â
    var parent: OSXTextField
    Â
    init(parent: OSXTextField) {
      self.parent = parent
    }
    Â
    //NSTextFieldDelegate methods would go here
    Â
  }
}
Â
This experience, all the hours I put into getting TextField to work, has also been a great reminder of the sunk-cost fallacy.
Comments / Feedback? Find me at mastodon.social/@MarcMasVi or @MarcMasVi
Marc