When wrapping UITextView or NSTextView in SwiftUI, writing user input back to a binding triggers a redundant update cycle. The common fix — comparing strings before applying updates — is expensive for large text. A better approach uses SwiftUI's transaction system to tag updates with an ObjectIdentifier of the originating text view. In updateUIView/updateNSView, the wrapper checks the transaction's custom value and skips the update if it originated from that same view. The custom transaction value is declared via the @Entry macro in a Transaction extension. This pattern generalizes to any UIView/NSView with gesture-driven state that feeds back into SwiftUI.

3m read timeFrom nilcoalescing.com
Post cover image
409 Impressions