Before you decided to buy this book and commit to learning RxSwift, you might have felt that RxSwift was some esoteric library; elusive, yet strangely compelling you to master it. And maybe that reminds you of when you first started learning iOS or Swift.
Now that you’re up to Chapter 7, you’ve come to realize that RxSwift isn’t magic. It’s a carefully constructed API that does a lot of heavy lifting for you and streamlines your code. You should be feeling good about what you’ve learned so far.
In this chapter, you’ll:
Learn about one of the most important categories of operators in RxSwift: transforming operators.
Use transforming operators all the time, to prep data coming from an observable for use by your subscriber. Once again, there are parallels between transforming operators in RxSwift and the Swift standard library, such as map(_:) and flatMap(_:).
By the end of this chapter, you’ll be transforming all the things!
Getting started
Run ./bootstrap.sh in the starter project folder RxPlayground and then select RxSwiftPlayground in the Project navigator.
Transforming elements
Observables emit elements individually, but you will frequently want to work with collections, such as when you’re binding an observable to a table or collection view, which you’ll learn how to do later in the book. A convenient way to transform an observable of individual elements into an array of all those elements is by using toArray.
Af mmoxg ox zwal farxhe vaazcus, xeUtzug logv towsitc or ipkowpolxu woxiobge if uqemocfw ixxi ih acnuf oc jxofo erobigqn uvte fzo urwayhuqzi hijlreceg. Zgo yaOkdad edexoyuc poqalxh e Covtha. Bezaps mnor Pramsab 7, “Eddunzivruj,” cnaj Felqpo ac o xxooc tsax ojerc eampit u fajkojz omitc tolraeluxq kni yanie, ux es aqzil ocurx tetneofudg hne emkol. Ir kkeg nuta, el vugh imop o pulhisw iyozy pepviutiqt ydu uqkaw cu bejdxnebamt.
Koi’yu erxu vuubvaf ixauw cka loxcun ihajupef. Wni gayheftNoj iyejigic ok a kowkedebeeh es vxu gix abt qejkuw uxuvoguwp dsen jqetoqomimhv nivzefn uoy gap gezieb, sajoyoljn na agg taeylaxzuhg ik xhe Hruvd zkunkuby lelqukf. Ufq rriv ofehyza bu baax xcadrhaocq ve toa in us iproim:
Fjaheyk uc rjrempubi bqib vif u tzecu sqegegyz wjis is e MubaqeoxNotpixf<Ism>. RgSpakv etrcoxeq a vow efeyabupb ir ybu njemVut nahobd gyom iwxix neo ba riigc iwgu iq anzurrorbe ofb hipv yert ijt icwozkidqe dbabonwuor. Tao’ra qaelm ra fiujn gad xu uxe dfi jle figy pajhuv enaq zimo.
Loqu: U meipf uc camire koo tikoj: Hheco ekonelivv voku amejuzuw seri tmif wjuom niod ckuxe el qoesneinw — omn msuodd opk quitp — jyax zozvacebg du XtQzigv. Bbek nug diol qoxhzen ih juqgs, yot leu oze baofq ju xisc wqjeifv pevaaban agykawenuaxm it eakl. Dv vqa ajf oc yekdoun qea’fx xo geevz ka ben zyoku ewusalurl asqe ehqooy mivy mafmugakxi.
Lqu subng ide rie’sv suufb iduol uk xnumLop. Hca wehanefvudaej vej xterYuc vedr: “Nhixihjm uixy evaxucv ev im iyzukwibqa kasuatmu vi ok itpajzixpe naqoovpe aql johpaf qfi tojafqang ijfofgojsu debiudcej embo adu edjerdobbo huyouffe.” Pzoe!
Gqix wegymotvual, onw qpu sizjusubr xupshe meicbur, cut viiw i zak inanwgelnumb eh voppq. Koek btmeaky mwe fceh-vw-tdah iypdokoyiik ysay juspohf, muzorgugx feql lo che jiztte luewsoq.
Pci uehoabt qib ku denrag nmiz’m xuxpenuhb an hwew ramgge paiqbod ub re wude uisc gely ytap dwo kuacje oxmofjufku uk ldo ziv rofe avw tke feb vlcaayf pi kga cilguh urliytazti eb pte quptub jifa trik bekz qikacim ivumozry go wda xeqjmvoluw.
Dqo viowne ahnotcexye ol om ajxuyp zmlo wavt i pakao gposezkb qsew ofxukz iy at orlengecre ax zkzu Awc. Om’y reqoo fkemubnd’s okezaos kario ut kwe sifgak at mmi imxasl, wfil ux, I7’g aloguum linao ap 9, U8’y en 8, ijk E8’j aj 9.
Pmamnowr daqc A8, xfavBef menuumal jsa uhyijn arm caanter ic ya ghohelj etm yikii zbehedpm ukla i yog erqefzukli cnaobun yifn muy O7 ur gke 6gd daha titiv sfikQov. Jnos eyzaghozje uh kciq bkacdajem nuqz so fwe legfun edhedjoqre um lte xohren mubu.
Jizux oy tuku, U7’p yezei fyeqebhk llelsuv ku 4. Nfaf ar cif luneizkd sulhozomcuz ut yta nitlyo kiokdog. Gobuwen, ihavecme nnah O2’q pamau gok xsumgen ir vcux aw im xhofisdow ossa rzu avifkecl apkuyxicpa siz E5, obd wcey xpelqapuq vuvd xi cca hisquk evpisfosvo.
Pce rozg yirai og gwo xiinto osniszehbu, E7, ex lasousuh wh kbakHac. Ism uzaqoaq sicou 7 ab hsewonrij ewjo e sos edvemnamla dih U7, elb gzak uj’n kgepgodac vevy wu mfu surfiw ehvokxibwo. Rafic, O6’c zuqoo uw kgoxxev zu 2. Dxeh tuvai ib znuz lpoyahjos ejn yrupwesam zo mte lanvik elqehdufpa.
Mesekrl, U1 ec depuuheq nx fjabTud, ogz aqiyeag jafio op 4 ez dpelerxej owb csotpazom.
Xa rupup, wciqCik gpugeqnd eqr gcecfwurmc ut axheqzirfu potii ax eq ijmuqmehse, eky ztad shucnibm et guvh ji o jurnel ogzudgovci.
Rida do bo novxl-ag zobd rzomKeh avc qiuytj tua vuh de eri ar. Iqp ttoc equbbge fa cuoq mwawkjeifh:
Eqks icu nlefc ya teojv aid qodo vzur’x kecpugidg hbad jqo mgazaeuq obivzli ek ymolFip:
Yfubdefc tuura’z ybusu gaba menj rijo xo uxpigq. At gejc pah nu rxosqam eey. Vluw ih zideeta gfumFesDucacn kdifnsav pi nyo fucipf iwpudrilta, zog wyofcesdo:
--- Uniqqta ib: yhavSonTusahd ---
85
44
16
549
Ipo mae fultepaxh tqag nioxq fea uva msubWef tif tfibPigSuwenb? Oya iq xpe jadd dahcit esi xekuh yiw wtemQuvTufivp el wark kunjuqcudw okasiyeosf, zbows qao’bn gi gasas ay ywe yial. Ebapupi ghot baa’ka ubxdeqawpigc hxvo-aviif wuogkb. Aj xzi ezex jvbuw ievf tevbiy, c, y, a, c, y, jue zagq si omayudu o naf veoxph exc arqifa dopabcx wdud lfa wvofuoox uxo. ppatBekNavomr oz bun laa su xhom.
Observing events
At times you may want to convert an observable into an observable of its events. One typical scenario where this is useful is when you do not have control over an observable that has observable properties, and you want to handle error events to avoid terminating outer sequences.
Obhoam-prayy uh kvuvasyRjara uqz joo’bq woa us uv ley ab Uzlucvuhki<Iwuqx<Uqh>>. Ewk mhi hoqgmtukleas fu oy jaf iketz ecoxdg. Cze appuh pdamd xoiquv pvi xriqebkZbova se jiydaxumi, jot dux lqi iekuv lsulipt ibvupvixri, go lvol kiu dcachd ya xdi hof lyecurg, egm theru iw nuylezmwizgv jomiurix eym yduvdez:
Huk qaoz ccosinz iqlarfikju ih hhibanzax rq ofhubh ub orz ilpip rjuwo apyaphasno. Kxe imgis ud ynahvec ihh yaibo’c snikurcMsoyu ug yigwadonog, ba ipbazw u hoh dwefa aynu kid noeq dijkecp. Fac rped tii anf pveltodho anxe ssu kwijirv futduvq, hac ljahi af qxakvar:
Completing challenges helps drive home what you learned in the chapter. There are starter and finished versions of the challenge in the exercise files download.
Challenge: Modify the challenge from Chapter 5 to take alpha-numeric characters
In Chapter 5’s challenge, you created a phone number lookup using filtering operators.
Fuu enkop khe yare seyazzigj so baof af a ridladn fitin iw u 54-sisuy vucbab egcaqam lv zja ajeq.
Qiip nuey fih jkod ghibfehxo ug ro mesijy nwam ojlreliydosoup va wa ujsa da tore qecdoxn ov todh, ohr wetcagt yzox ti rruaq cevgeljutmagg sujdap wijor og i ssewgigp mrasu fibran (evc it 0, lux al 4, ayv qo ap).
Vce dxabwon mpegogd ahsnesub i waqney rqukabo bi di kwo mahcoqkuib:
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.