Swift Combine: Map Operator Basic of Basics

KD Knowledge Diet
2 min readOct 24, 2022

--

Map operator exists in almost any language. Understanding it in modern programming is essential. It also exists in the Combine Framework. Let’s learn it! Note that map operator belongs to Transformation Operator by its characteristics

What is map operator

The map operator that is available in the combine framework works exactly the same way as it is available in Swift Language.

Map operator in different languages is also called the transformation operator because it takes an array of items and then transforms it in two completely different set of items.

Example

let formatter = NumberFormatter(0
formatter.numberStyle = .spellOut
[1, 2, 3, 4, 5, 6].publisher
map {
return formatter.string(from: NSNumber(integerLiteral: $0))
}
.sink {
print($0)
// one
// two
// three
// four
// five
// six
}

Let’s say if I have an array of numbers [1, 2, 3, 4, 5, 6]. If I want to convert or transform these array of numbers into the English Language equivalent of those numbers, it will become like this [”one”, “two”, “three”, “four”, “five”, “six”]

Map Operator takes in an array of items, it can map over it and then produces a completely different type of items.

Conclusion

Map Operator takes an array of items and then transforms it in two completely different set of items.

--

--

KD Knowledge Diet
KD Knowledge Diet

Written by KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!

No responses yet