How do I debounce text input in SwiftUI with Swift?

This example demonstrates how to debounce text input in SwiftUI using Combine.
SwiftUI, Combine, Debounce, Text Input, Reactive Programming

import SwiftUI
import Combine

struct DebounceExample: View {
    @State private var searchText: String = ""
    @State private var debouncedText: String = ""
    private var cancellable: AnyCancellable?

    var body: some View {
        VStack {
            TextField("Search...", text: $searchText)
                .padding()
                .textFieldStyle(RoundedBorderTextFieldStyle())
            
            Text("Debounced Text: \(debouncedText)")
                .padding()
        }
        .onAppear {
            setupDebounce()
        }
    }
    
    private func setupDebounce() {
        cancellable = $searchText
            .debounce(for: .milliseconds(300), scheduler: RunLoop.main)
            .removeDuplicates()
            .sink { value in
                debouncedText = value
            }
    }
}

struct DebounceExample_Previews: PreviewProvider {
    static var previews: some View {
        DebounceExample()
    }
}
    

SwiftUI Combine Debounce Text Input Reactive Programming