What is Combine in Swift?

Answer

Combine is Apple's reactive programming framework (Swift-native, iOS 13+) for processing values over time through a Publisher-Subscriber model. It handles asynchronous events (network responses, user input, notifications) in a declarative, composable way. Core concepts: Publisher: produces values over time. Subscriber: receives and reacts to values. Operators: transform, filter, combine publishers. Cancellable: represents a subscription — cancel to stop receiving values. Basic example: import Combine var cancellables = Set<AnyCancellable>() // Publisher from notification: NotificationCenter.default.publisher(for: UITextField.textDidChangeNotification) .compactMap { ($0.object as? UITextField)?.text } .filter { $0.count > 2 } .debounce(for: .milliseconds(300), scheduler: RunLoop.main) .removeDuplicates() .sink { [weak self] text in self?.viewModel.search(text) } .store(in: &cancellables). Common operators: map, flatMap, filter, reduce, merge, zip, combineLatest, debounce, throttle, retry, catch, receive(on:), subscribe(on:), removeDuplicates, assign(to:on:). @Published + Combine: class SearchViewModel: ObservableObject { @Published var searchText = "" @Published var results: [User] = [] init() { $searchText .debounce(for: .milliseconds(300), scheduler: RunLoop.main) .removeDuplicates() .flatMap { text in APIService.search(text) } .receive(on: RunLoop.main) .assign(to: &$results) } }. Combine vs async/await: both are valid; Combine excels at reactive streams with multiple values; async/await is simpler for single-value async operations.