Operators are functions in Combine that process or transform values emitted by a publisher before they reach the subscriber.
They sit between the publisher and the subscriber inside the Combine pipeline.
Conceptually:
Publisher → Operators → Subscriber
Operators allow developers to modify, filter, combine, or transform data streams.
let numbers = [1, 2, 2, 3, 4, 5].publisher
numbers
.filter { $0 % 2 == 0 }
.removeDuplicates()
.map { $0 * 10 }
.sink { value in
print(value)
}
This pipeline processes values step by step before delivering them to the subscriber.
The publisher emits values:
1, 2, 2, 3, 4, 5
These values pass through each operator sequentially.
filter.filter { $0 % 2 == 0 }
The filter operator allows only values that satisfy a condition to pass through the pipeline.
In this example, only even numbers pass.