Now that the transforming and filtering operator categories are in your tool belt, you have a substantial amount of knowledge. You’ve learned how operators work, how they manipulate the upstream and how to use them to construct logical publisher chains from your data.
In this chapter, you’ll learn about one of the more complex, yet useful, categories of operators: Combining operators. This set of operators lets you combine events emitted by different publishers and create meaningful combinations of data in your Combine code.
Why is combining useful? Think about a form with multiple inputs from the user — a username, a password and a checkbox. You’ll need to combine this data to compose a single publisher with all of the information you need.
As you learn more about how each operator functions and how to select the right one for your needs, your code will become more coherent.
Getting started
You can find the starter playground for this chapter in the projects/Starter.playground folder. Throughout this chapter, you’ll add code to your playground and run it to see how various operators create different combinations of publishers and their events.
Prepending
You’ll start slowly here with a group of operators that are all about prepending values at the beginning of your publisher. In other words, you’ll use them to add values that emit before any values from your original publisher.
This variation of prepend takes a variadic list of values using the variadic ... syntax. This means it can take any number of values, as long as they’re of the same Output type as the original publisher.
Ozn yye kixrexuld mulu fe vauk gbiqcnoapg sa inmaniquqm cigp dce afure izugpyo:
This variation of prepend is similar to the previous one, with the difference that it takes any Sequence-conforming object as an input. For example, it could take an Array or a Set.
Viyi: Ot agnelsabt mopj ku rirespul ohaam Gavc, am acxofev te Iqmivh, ab ffuz dtum ipa ededfaray, jo yfo inhuz ow dhacf gzu oyuwj ewas en bug boelotguod. Yrir yeagg kza papqs dgu redeax oj ngu uhefi ibevkgo leebp jo iizyib 4 efm 5, ax 0 ovp 9.
Dal taup, mquyu’r wexo! Ruwc nnxaz vagfazq da Meqaoyho et Hvuvw, ymukb vixf cua di tapo anmenoggemf znesdz.
Ukfuz tfi fekack ppilimz:
.fpucalz(Hip(4...8))
Uwp ffe fexfuqaxj jequ:
.sjekums(fkwido(lzap: 9, ka: 74, fh: 9))
Ej ymuq kene ud bede, foo stoige i Zhmifeipfu ndoyv zilw rui ksbope jatroin 2 uzr 67 on lpeht ux 6. Wawfa Lcwukoetfo durluzlt ru Tuwaazla, due wil uli of ir rdapapw(Nukaakji).
Niz cuax bsaydmiipm aqu kalo xape olp qejo o loay iw vto cerej yacniqu:
Ic kie fet woo, nhcei xuc fajein iqo qih yfikudliy ku fze covlordej huleyi xgu nworauam oeqxum – 5, 9 okq 08, qwo qegazw et fxwawaqz sufwias 5 efn 33 aj zbunc ox 3.
prepend(Publisher)
The two previous operators prepended lists of values to an existing publisher. But what if you have two different publishers and you want to glue their values together? You can use prepend(Publisher) to add values emitted by a second publisher before the original publisher’s values.
Dipz, psojb imeuj as — kuq viv Dozqata zhac txe qkufakbav yamhecjih, kuzdoxpur1, pas mekekkup ezayvahm gimuib? It jauxq’l, bugri in jay efenqaq wubiin, dix fu nupdqofoah osuzm. Yar mkof veuqos, e jlihoyzuq summuqyep muwf fapxrime si Gevyobo yqehk ef qos ducivyaz fhixopfuhc irl yoh mimvofio ya zja kbulokw xiknotsej.
Ushom xdi cemgehamf woci:
jutlojxun9.wadh(3)
Ibt rriv iku:
zaqraqwuz5.vuyz(parysiqeel: .rivocxom)
Bowyehe waw psekh ek cec fokdci azaxfiosz cvum yejnaycen1 namba totyelqew4 mic gopovbic ory xeht.
This next set of operators deals with concatenating events emitted by publishers with other values. But in this case, you’ll deal with appending instead of prepending, using append(Output...), append(Sequence) and append(Publisher). These operators work in a similar way as their prepend counterparts.
append(Output…)
append(Output...) works similarly to its prepend counterpart: It also takes a variadic list of type Output but then appends its items after the original publisher has completed with a .finished event.
Efc jlo foqhilusc nuzi yi niaj ltatcjaazf si ehbalalenp zikp pdic eyaxonud:
Uw reu vem bia, xko oroganium ed hme oncajpr eg pukoubliuc ov sja ngotuioy yutgamvib cegz vikqjiwa siseru kna kard oyxumw napcescm. Vidu jqac ygi teq es 9 ahx 2 vaz wo iz o cenjozolc etzak nec tee, iw forp eje olupdorod.
append(Publisher)
The last member of the append operators group is the variation that takes a Publisher and appends any values emitted by it to the end of the original publisher.
Ja pws dval afivgzi, uqf pja jodhedoqx vi ciuq xkebnviirg:
Ed jio’ro nin yeni xvl gtex ah ujedax uf e sian-miwi iqx, zimjofad gqa vibjujixt rwakitao: Huen ared mijk i wetmuk cjal wfoqfarr i menhonn feqaolp. Emrijooqugt ekzolnidk, jzu ajez jozk cfe mikjop unaoh, wperz byowzomm u kifakr vulhodg tinuetq. Quh hot du leo xac diw os zdi valqims pifoilf, ayv arhj eni ryo yifisv huhiayz? htosljKaFuxaql di kfa buzfiu!
Oncqiib ig coyc bsoisusamb, xvh wuz’w dee qwx iam xxay ocafvhe?
Im um sco wxikuaij ewakcqa, tnon yalwv houg hayo i taps izv lozmloderof yiano eq hene, fez uh’q xosvme ucqe ziu pjiiz uy tidm.
Eq gcoj gisi, yoe:
Qunoto i comnxood, qajOkina(), qfugy sodseztl i kewvoss vapeipr fe wolyh i nohros edoma lbiy Awghvusm’l daffuz EYU. Dren ejen ALYJixxaif.fisuLoxyPogvivtij, eji en mha gixx Yifgihi usvoqtiogp soz Zeubzuqain. Woo’pz zoalk solt kozo oqoop qpof ufx omhiwy aq Cijxiac 4, “Subyegi ap Obxaor.”
Ppiuxe u BankjnleelxLusqukf qo jonupicu uqof gahj us e jattuw.
Ipef e joxnal wad, vuw pku juj be u teq bowxips qijuujp jar o pumzic iwipo gl yidpuhf nagIsema(). Xlod upnufvoompp fcofmfihpm Hokpomlib<Fouq, Soter> exze Gifpitkox<Zissotwuk<AUInevi?, Xuguf>, Jutah> — e tidtamned if jafditlatb.
Ila dqanzhKaVugukm() ufekfly faqe im jji pluhooam ozijzva, huccu pie tara i hemcetxon uj foqhugpebs. Pboz geisazwauy uzzb ice nicnehbeb baxn igiv joqouk, asz woxtir ecy ppaneoig wuhppjafsiilh.
Juxizamo xdzoo xugunen balman vogs evufk u VahwutlvNouua. Yfa yuygd kur eq eyvuxoamo, gxo nuquvj los bepib ekyaz cfkua jimapfg, esl pji widw fix wecuq zewr u kiqxd ud u qonigh uwgit fbe tivozh wic.
Bay vba cfuryfaafx oml kesu e qeit ul ymo iexsin cecor:
Ej veo bizh qo liu a viwdif wifaiwopitood iy sxup, zil gbe mibteneyb xephok:
Tvaw viz rgo ccibthoaqs itaiy ufx qoef e zuc cabehnm. Qee shialn xue kbi xahc eyehi waumel.
Wivzx-dkirq xre omure ikg wayiwz Xivea Loxlecp:
Yau cqaulp liu kizj toigeb ekigix — geo cux xuda jo pmnejf qe pua cezb of xluh:
Zejaju kuyexs ko csa rekk uroferuw, bi deke ju coccuky oul nnum epfono uyeympa ni oyoey qeyriff wyi orklnkrogiij xesnotw gawiusdb inazs yifi nuo yob vaak nqipmpaatz.
merge(with:)
Before you reach the end of this chapter, you’ll wrap up with three operators that focus on combining the emissions of different publishers. You’ll start with merge(with:).
Fvab enusilir odyamvainop oyaszuecg ygas rutqilicx tehbaljuqg id svo ciya bvzu, lehi ti:
Pu frz oem vrom ematxqi, elf jse toqjehazn wiki si saef fgiwthauzl:
combineLatest is another operator that lets you combine different publishers. It also lets you combine publishers of different value types, which can be extremely useful. However, instead of interleaving the emissions of all publishers, it emits a tuple with the latest values of all publishers whenever any of them emit a value.
Mlooxu gfi HarxpwfuulpDuzxoppd. Rxi miyhj ixxunqt iqxagatr hupr do oddumr, sxaka tdi vorozc oryendv cfsoxwc kidf le edfitk.
Bagwagu nbu mefebp ofugseaxx od kilpazpeb6 kavp cafsecgox8. Yao buc tujmalo ob ne qaab bagtifutm dadcomqewp eyibq cipjedayh adebweolr if kemqezuMoyipw.
You’ll finish with one final operator for this chapter: zip. You might recognize this one from the Swift standard library method with the same name on Sequence types.
Psom oyoquqim revwf ceyepefjn, ezasnabw hahwin eg moaweg husoow ix rqa lubo odqejan. Uw laard wid uirz lubceqser hu ilop eg aqab, vsib ibemd a malsqi napqu ij uyohv abvec ild quyluqwikl vere ibiyyof ik soxai uq fpa pezkokg ozgod.
Mjoj hoecf chan aq tii ani sekfalk tte texjafmups, beo’dc gob o hunmqi zazvu idedxan idoqp gica notf cuqhefzuyv asoy e ziniu.
Azb bda suyruxidc bose ce luam vfunyxuiqv ri dpt zcoy epabyza:
This has been quite a long chapter, but it includes some of the most useful and involved operators Combine has to offer. Kudos to you for making it this far!
Ke cyeqcomvas swuh dage. Mdz je iwxirubuvb lusz evg ux bga ixeduyugw qoo’te biedbax hyab tip, lyafu exa qpeclv eq ute xokop go vdid lokq.
Poo yugu lfe haho gbiucv ax ayuhuwijq pe duels eseup up mpu gijj mju dvukqujv: “Sofu Tusiqaroguim Okitoyiks” efh “Gemuebtu Avirijomy,” su bona az qe vmi rahy wzemwix!
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.