Apple's reactive programming framework for processing asynchronous events over time. Combine provides a declarative Swift API for handling values and events that change over time.
of iOS users worldwide can use Combine (iOS 13+ required)
| iOS Version | Market Share | Status |
|---|---|---|
| iOS 18.7 | 30.3% | Supported |
| iOS 18.6 | 29.8% | Supported |
| iOS 26.1 | 10.7% | Supported |
| iOS 18.5 | 6.0% | Supported |
| iOS 16.7 | 2.3% | Supported |
| iOS 26.2 | 2.0% | Supported |
| iOS 18.3 | 1.8% | Supported |
| iOS 15.8 | 1.8% | Supported |
| iOS 17.6 | 1.7% | Supported |
| iOS 26.0 | 1.3% | Supported |
Declarative reactive programming
Built-in operators for data transformation
Publisher and Subscriber protocols
Automatic memory management with AnyCancellable
Integration with SwiftUI and async/await
Thread safety and error handling
Create and subscribe to a simple publisher
import Combine
let publisher = [1, 2, 3, 4, 5].publisher
let cancellable = publisher
.map { $0 * 2 }
.filter { $0 > 5 }
.sink { value in
print("Received: \(value)")
}
// Prints: Received: 6, 8, 10Fetch data from an API using URLSession and Combine
import Combine
import Foundation
struct User: Codable {
let id: Int
let name: String
}
func fetchUser(id: Int) -> AnyPublisher<User, Error> {
let url = URL(string: "https://api.example.com/users/\(id)")!
return URLSession.shared
.dataTaskPublisher(for: url)
.map(\.data)
.decode(type: User.self, decoder: JSONDecoder())
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
// Usage
let cancellable = fetchUser(id: 1)
.sink(
receiveCompletion: { completion in
if case .failure(let error) = completion {
print("Error: \(error)")
}
},
receiveValue: { user in
print("User: \(user.name)")
}
)Use @Published for reactive property updates
import Combine
class ViewModel: ObservableObject {
@Published var searchText = ""
@Published var results: [String] = []
private var cancellables = Set<AnyCancellable>()
init() {
$searchText
.debounce(for: .milliseconds(300), scheduler: DispatchQueue.main)
.removeDuplicates()
.sink { [weak self] text in
self?.performSearch(text)
}
.store(in: &cancellables)
}
private func performSearch(_ text: String) {
// Perform search with debounced text
results = ["Result 1", "Result 2"]
}
}Apple's reactive programming framework for processing asynchronous events over time. Combine provides a declarative Swift API for handling values and events that change over time.
Combine is available on iOS 13 and later. Currently, 97.2% of iOS users worldwide can use this framework.
Related frameworks include Swiftui, Urlsession. Each has different capabilities and iOS version requirements.