Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

Clearing Change Count in FileDocument?

I'm playing with a simple document-based application with TextEditor for macOS. In Cocoa, NSViewController can call updateChangeCount(_:) to clear document changes in NSDocument. I wonder SwiftUI's View has access to the same function? Hopefully, I would like to manually set the change count to zero if the user clears text in TextEditor. I bet SwiftUI doesn't have it. Thanks.

import SwiftUI

struct ContentView: View {
    @Binding var document: SampleDocumentApp
    
    var body: some View {
        VStack {
            TextEditor(text: $document.text)
                .onChange(of: document.text) { _, _ in
                    guard !document.text.isEmpty else {
                        return
                    }
                    
                    // clear change count //
                }
        }
        .frame(width: 360, height: 240)
    }
}

You could do that by using undoManagerto track changes instead of explicit change counts using onChange and removeAllActions to clear the stack.

I see. I didn't think about it. Thank you, DTS Engineer. I'll try it later. I tried using undoManager with TextEdit under macOS two days ago. The macOS application consistently crashed when I tried to 'redo' it. If I remember correctly, the exact same lines of code didn't crash under iOS.

Clearing Change Count in FileDocument?
 
 
Q