Transforming / Map Operators in Combine Framework

Nilaakash Singh
Dev Genius
Published in
4 min readApr 23, 2022

--

In this article we are going to talk about Transforming which is also called Map operators of Combine Framework in iOS, these operators provide ways to transform data just like collection and filter operators does, they work on a series of data.

Use Cases: It is a common programming task to map sets of data. The Combine framework offers many ways to map, filter, and reduce sequences of data, these operations are chained together and a transformation is applied to each element as it passes through the operators, rather than doing the operation on sets of data.
I would like to introduce some of the operators in this article and invite you to explore many more that is provided by Combine.

Prerequisites:

  • Xcode 11 and above which supports Combine framework
  • Basic knowledge of Publishers, Subscribers, and Subjects.

Let’s get started:

Collect:

The collect operator is a publisher that publishes results as an array. This is very useful if you want to break data into contagious data types so that it can be used in a sequence manner. We can also pass an integer as a parameter to chunk the output.

Collect Transforming Operator in Combine Framework.

In the above example, We have used the collect operator twice to show how we can use collect with and without limit.
On Line 3, if we don’t have a limit, The publisher will return all the data in a single array.
On Line 6, if we set a limit to the collection operator, The publisher will return data in chunks.

Replace Nil:

As the name suggests this operator is used to replace nil from the set of datasets with provided data. It can be very helpful in case where we get datasets which is not consistent. We can set a fallback by replacing nil with such datasets.

Replace Nil Transforming Operator in Combine Framework.

In the above example, We have a small array of alphabets and nil as of the elements. We are making the array as publisher and using “replaceNil” operator with the element “*”, the result in the sink will always have a value that's why it is force unwrapped.

Scan:

Scan operator is used to transforming the upstream data into certain sequences of values depending upon the requirement. We have two parameters after we use the scan operator:
a. Partial result: First parameter which can be easily accessed on every loop.
b. Current element: The second parameter can be used with partial results to a specified pattern.
The best part of the scan operator is we can set our return type as shown below in two examples:

Scan Transforming Operator in Combine Framework.

In the above example, we have used a scan operator to return array and integer with different examples.
On Line 4 and Line 9 we can see the usage of the scan operator and in the bracket, we have entered the initial value. In the case of an array, it is an empty array and in the case of an integer, it is 0.
When we start the closure we get the number and value. The number is previous values whereas the value is the current element instance. We have our custom return type [Int] and Int as per that we are performing operations on Line 5 and 10 respectively.

Map:

Map operator in Combine works similarly as Map in Swift. It provides a closure where we can add our logic to map data in data sets as per requirement. We are going to look at the map use case twice.
Let's look at the first example below:

Map Transforming Operating in Combine Framework.

In the above example, we are using the map operator to convert the array of numbers with spell-out characters. This is just one such use case of map operator. We can also use map for date formatting, adding custom logic to data sets, and so on.

Map Keypath:

We can also use Map operator to map keys of object. We have seen this usage many times in our day to day programming life but with swift map operator. Combine also provides same sort of map mechanism on publisher so that we can directly operate on upstream.

Map KeyPath Transforming Operator in Combine Framework.

In the above example, we have made an object called as Point which has two properties i.e x and y, both are Integer.
We are using pass through subject as publisher on line 8 and subscribing it on Line 9. However nothing will print till Line 12 where we actually send object to downstream.
This is one of the simplest use case for Map and you can find it similar to swift map.

Flat Map:

Flat Map operator has similar syntax as flatmap in swift. However, there is a significance difference working with it. Flat map in combine framework is used to combine upstream publishers to single down stream publisher up to max publisher. Max publisher can also be unlimited if not specified.

Flat Map Transforming Operator in Combine Framework.

In the above example, we are using current value subject as office and 2 objects i.e Village and Town office.
- On Line 17 we have used flat map for number of employee on office subject which was initialized to track village office on Line 15.
- On Line 20 we have combined now town office object to office subject
- Now whenever we change number of employee on both of the object, it will print number of employee on Line 19.

That’s it from Transforming / Map Operators in Combine Framework. I will be back with one of the most interesting operators in Combine called as “Combining Operators”.

Where to go from here:

We can try these operators at a time when we are using subscribers to fetch data from the database or server, this will help us to get transformed data and avoid data that is not required at the beginning. You can find the different combined operator and their usage in my GitHub repository.

More Reads:
Sequence operators in Combine: https://blog.devgenius.io/sequence-operators-in-combine-framework-c942182150a

Filtering Operators in Combine Framework: https://medium.com/dev-genius/filtering-operators-in-combine-framework-d51798d4e2a9

Share this blog to spread the knowledge | Do Follow, and CLAP if you Liked it! Happy Coding :)

Add me on Linkedin.

--

--

Senior iOS Engineer at Loblaw Digital | Swift | SwiftUI | Combine | Swift Async | Blogger.