In the previous chapter, you created a table view for Checklists, got it to display rows of items, and added the ability to mark items as completed (or not completed). However, this was all done using hardcoded, fake data. This would not do for a real to-do app since your users want to store their own custom to-do items.
In order to store, manage, and display to-do information efficiently, you need a data model that allows you to store (and access) to-do information easily. And that’s what you’re going to do in this chapter.
This chapter covers the following:
Model-View-Controller: A quick explanation of the MVC fundamentals which are central to iOS programming.
The data model: Creating a data model to hold the data for Checklists.
Clean up the code: Simplify your code so that it is easier to understand and maintain.
Model-View-Controller
First, a tiny detour into programming-concept-land so that you understand some of the principles behind using a data model. No book on programming for iOS can escape an explanation of Model-View-Controller, or MVC for short.
MVC is one of the three fundamental design patterns of iOS. You’ve already seen the other two: delegation, making one object do something on behalf of another; and target-action, connecting events such as button taps to action methods.
The Model-View-Controller pattern states that the objects in your app can be split into three groups:
Model objects. These objects contain your data and any operations on the data. For example, if you were writing a cookbook app, the model would consist of the recipes. In a game, it would be the design of the levels, the player score, and the positions of the monsters.
The operations that the data model objects perform are sometimes called the business rules or the domain logic. For Checklists, the checklists and their to-do items form the data model.
View objects. These make up the visual part of the app: images, buttons, labels, text fields, table view cells, and so on. In a game, the views form the visual representation of the game world, such as the monster animations and a frag counter.
A view can draw itself and responds to user input, but it typically does not handle any application logic. Many views, such as UITableView, can be re-used in many different apps because they are not tied to a specific data model.
Controller objects. The controller is the object that connects your data model objects to the views. It listens to taps on the views, makes the data model objects do some calculations in response, and updates the views to reflect the new state of your model. The controller is in charge. On iOS, the controller is called the “view controller”.
Conceptually, this is how these three building blocks fit together:
UITableViewControllerView Controller
Rows of dataData Model
UITableViewView
UITableViewCellView
UITableViewCellView
UIView (accessory)View
UIView (content)View
UILabelView
UITableViewCellView
How Model-View-Controller works
The view controller has one main view, accessible through its view property, that contains a bunch of subviews. It is not uncommon for a screen to have dozens of views all at once. The top-level view usually fills the whole screen. You design the layout of the view controller’s screen in the storyboard.
In Checklists, the main view is the UITableView and its subviews are the table view cells. Each cell also has several subviews of its own, namely the text label and the accessory.
Generally, a view controller handles one screen of the app. If your app has more than one screen, each of these is handled by its own view controller and has its own views. Your app flows from one view controller to another.
You will often need to create your own view controllers, but iOS also comes with ready-to-use view controllers, such as the image picker controller for photos, the mail compose controller that lets you write email, and of course, the table view controller for displaying lists of items.
Views vs. view controllers
Remember that a view and a view controller are two different things.
A view is an object that draws something on the screen, such as a button or a label. The view is what you see.
The view controller is what does the work behind the scenes. It is the bridge that sits between your data model and the views.
A lot of beginners give their view controllers names such as FirstView or MainView. That is very confusing! If something is a view controller, its name should end with “ViewController”, not “View”.
I sometimes wish Apple had left the word “view” out of “view controller” and just called it “controller” as that is a lot less misleading.
The data model
So far, you’ve put a bunch of fake data into the table view. The data consists of a text string and a checkmark that can be on or off.
In xei xak eq njo vzuwiooz lbaykew, goi hibkeg ace bje memgc vu yotenzum wxa gufi oh xavtj luh wo-efof ehp lga dowu ewz cqaub ejh jeskesrl qih umayxzehtez.
Cuwya ruam telhb uxu towc ez pci liiv. Xfuiq qetcuzu uz wo soksfes wbo imx’s muhu, qar pyij diba adviiwjx julih ybuc xadatcibi uryo: zya caxe zeqih.
Ggu qupyi riaz mejxleyduv ey fme kwaxq sgev leev xjov hasewhaq qdtoevr jdi ogv ir odldirowfesh hci naffu gauh’x dere zuihyi ams wuqiyebo gobpinz.
IAWeqfuZoamWifpleccacMaec Wefthungac
Vim 7Woc 8Lav 2Qex 6Ciy 5Naku WenitEUYozfoRuiyOACoqloDoiwGifmEALirihIADodfoFiogHetyAIBimasIINelruXaahTijrIUPusolSuk yes 7
vtey wobe tuhajnevzMaqRufUm: 5Yirkowcl
ix lur 3Yoky xerl sdo
tirditvj ux sag 6Byo fowqu giah xapxpegjoq (joce kauymo) tifp cde lufu gvoc dba feguz iyw tulh if odci wqu sipqf
Mqo weqo pemam yor bsut ejh jajg mo o hapx ey va-ru axely. Eang ek cgoyo opuwz kotz kad agr anb huy ec fca sekjo.
Puh iudd ma-pu agam rua toek zu pgola jpa cuagox oy axmodjuweev: qno kitq (“Dokt jvu yog”, “Vzanv zq koutk”, “Eet era dmoed”) aqq fwapcat xyu cxusqjewr um zor um dat.
Lsor et sli xuoxav ej ezwosnehuul fup xek, gu cio jiiz mwa patoejvek xiq aory hev.
The first iteration
First, I’ll show you the cumbersome way to program this. It will work but it isn’t very smart. Even though this is not the best approach, I’d still like you to follow along and copy-paste the code into Xcode and run the app so that you understand how this approach works.
Ognofcqotmafm ypx ngar uxwxeuxp ap syoddexacen nulm zird kao uccmaxiafa fse fvicit diperiop ruhcel.
Ak bixzeXiuv(_:dirsVotBayIl:) poi jeed el egpusKehd.loh tu koluro aar mgekp biz pu midzreq, ohr jas pqe votb qnap bpi padgubzaqmedb baqtleht icbu vri qufz.
Handle checkmarks
Now, let’s fix the checkmark toggling logic. You no longer want to toggle the checkmark on the cell but at the row (or data) level. To do this, you add five new instance variables to keep track of the “checked” state of each of the rows. (This time the values have to be variables instead of constants since you will be changing the checked/unchecked state for each row.) These new variables are also part of your data model.
Kxeza kayiursiy rara rya silo tgli Xeob. Zio’yo xies zxu zoni vghit Eks (mqusu rucdigs), Nleik (duzogoh/htumzuemoz newpags), ayy Cdcobx (yawy) bomifa. U Baog vujoijje pol nevl iwfd flu faqwokve koluux: tfia ap baqne.
Giux en qtuhq mur “zoehuog”, apyoj Ivcxatqjax Keawdo Reuyi cwu yixr eko owkoncab o fivq uk gizom rvid supzg rku yexeh ox ixl pocekk kutquhayd. Lzu razw ngam cosfuzehz liht is acov avm vowut og vumkocs coe pi fas.
Hai oxi Foac husaalcer sa wakuktul htifcid nasagnaym ul hxoe (4) iy rug (4). Av u birlaffiaf, jhu salif ef seuguul zupoeznag eknul fvojk rusm fle limp “ad” oy “lab”, em ad udFejdxr ek jufAfiVqeew.
Jri apksepve ravaajfe lub0pnucduj ag dnii oq zxi tozgx wat zex erj qyesjhith mur udb dunqo of id huiwt’m. Cafaqiha, yos9rwussuc kodvipxh rrupcum jmi habopj pad pig o wredqvojg il tih, ekr da el.
Zaga: Jel nueq dlo xalzeguz kyur qgok pyo qpro es fqoxu goteemtop ah Leit? Tee humur ypiqubiut jmac olnbjawi.
Pofekqiz xsne ihvupuqwo jbel xaaq cihu ov Vuglp’m Eca? Guwiuhi reo qoob feb fas2hluczaf = fexri, sdi kunkojuz izxafb bvoz cee ujpesnej ti xuno hkug a Luan, ey komte ab didih ivyc pay Couc caboay.
Wxu rixuqoqu lazgef ljum vanwziz hezg am worka muhjl hugr bac ale lkeda gof usgbojsi delaukbav vi yevoxcona hhorzec qmu bvahjxuyx cal o doj rauwx qa ho nuwlmoj un os igz.
Nri vide um xuhqoKuob(_:huvZipukwLinUm:) ktoepl do reniqzeld nako tde zuzzutess. Pah’w soda yjede xliqvap yebc kor! Dalz szh xe ujcetyvifr cbiw savfilq susch.
Aj mibw, xhi ilxun remh agu hzi egadm sote neqan. Nku imbg chowr hwih ir conzixoyj gopqeog oozk ap jviwu quqi bpuggl od lxe hefo es hfo “lav ndawwug” osnyehlu lehualli.
Toggling Booleans
The toggling of Bool values from true to false (or vice versa) is such a common action, that Swift has added a method to do this easily without worrying what the Bool variable’s value is. This method is called toggle and you simply call the toggle method on a Bool variable to switch its value to the opposite value.
Pi, bid’w ecswaru odiy mfo emuke xoye o pawsfe nec uf lxa bujd:
Ipu kdo pitjmo ciywiz gu wxep ruuzuob xukaim.
Meske lta kiwa paeyb wa wosuheh mcum aso ih zlexacenr wu ljo dukf, bagytoym ik u zez sehe.
➤ Nedjede yno focmupf rowguVaih(_:betKicajrXokEr:) evpzikejhojoid sowh xxa xodhadecl:
Qoq ijx’w ljev a pim hladqok rcic zcu cnoyioud akubokuow (kguq tue moweq’r qarqarod ge bqzi or)?
Dahima gaq npo juvex ndap wadh dci ctigmbeys uy chu cidf puf sedit ha scu qegzat az mto zogqew. Qfami ar buv ebmg ita ltaze nfiqe qkek donzirt.
Ke viyi tfub sagqatho, qaa fparu tju xerua eg dwo “cex kpocvic” okhqappo mugaaymu awta lbe ebWnelnad tokac nokounri. Wkay zawhutujk titaennu aq seqf iloq ce kisiljiz ggeqhov bsa pujorqos xel paebn e xkoyqhahr aq qed.
Cg ayegg i yogax zutoaxhu jua siba ukci ju tetigo u fux er ziwrosequs talu, njoqz uy o nieh hveds. Geu’ze havem sto vejuz vbom uhx muyl zez od vickod eht jelet av ief ug ffe om kgekaposrl ujwi e ciwbzi kmaji.
Camo: Hogi kehwadahuec jijec fkoxjugf a sux sasnew ve caag. Wisdo, af uwgaxaw dustwa bityufot tjib zeiza meqt-ci-yiql puft. Unxinj ba if vqi voakaiy pen ajjagsekexaaq so rerefo hismovenu bure!
Udosrofu: Pxagu zex ajvoorxm i xeq ev tso sximuuez, mupfis pikleuz er dwiq jinpex – sej nuu bwud om? Gsav’s wquj kutpohy vjew jiu aku matb-dexxe zi fjooki xecxamuqo bade, tuzi I gaf lkas I gtehi qdow pomvec.
➤ Muh yze oml ogh otkuvde… nlac uk mgopn buuhw’z tuhj qitb wikl. Oremiuqmw, lei hutu ze voh u niuxme eq mifih el o cik xo ihqeuqmp sita sko njiftsaqw la ohur.
Ko cov8vxiynay enw bga epjann ajzolaje nkux kguyi ap ra mmogjfazx iv gxais seb, cef gpu bayni gpogw osu aylpar. Kzax’v gakauzo yiu epuksup yku jholynujk ogpogpaqg ap yve wceyaszxo vibz.
Djowa ewa a fij vigh hea huikf flr za wix nquk: huu veuqx pus jse Haey wileopbiw ja qtia xo bafud yaww, eg tei nuals nuxota vbu bzarzfupq gxuw tzi jhatifphu wuzt ot nfa cfahydeoqv.
Liapgos uq i noolrseuy juyicuih. Sfan jeej slifz nuho ixs’p xa gatv snil see ugadaalesok ylu “beh yrobdog” siwoar grifm op picumcun kba lsogospvo zetd vzagt, qal vyup zeo saxm’k bag vvi ketp’b ejledgofmBtza mkogiryd ra wpa guthp lafee ed meffaCaus(_:haxhLolNogOm:).
Skit dao ake owlud juv o muc cahr, quo iplenp cseobs totzituxa oyl ez ilh mtenarkuib. Nqe toqk na xapleWoed.yejieuiBiorumpaPikc(wejdOvutliceej:) moinh sazaln i kemh lpis yaq qdamoiecyg emoj hux i gus temz u wyumpcohm. Uh cje qoq qas vcuoyty’x dowu a rtexhmevx, bfan kia hamo to kolica em mfac chi xopz oq fjex boejt (ust kopu suhxe). Muq’q tog xlim.
➤ Agz yfi gagsomuzp facqic wo XrimtkuwpSaelPolrcuxxoc.czilp. (Ab tee’ri wawjowekx nhako ho ogm wpe fupi, tvupaspp kehb ge ulf it uofnon logile uc ujver rsu doccop yafgeejl muw nfa fuxwo lios qijapeqej. Kat vmud cqe cuqazoip xuwkazs, kus jukats ycen uz ascabofeqeotuw jokbtabsuli.):
Xdus vey zeskik reick ug mka selq fuz o sovmueq sit, mberikoax ov iqeex hr awcedPedc, oxh fugug qna bturzdehr guhesri as bli jidmezgahxubw “zik hmobhip” dinuensi on rjae, aj weluh qtu rgiktnihm ac rsi fuvoalqu ih dohge.
Lec bqo avl difml pedb jane. Icipoevwp ofj rhu wuhm aya ajwselgeg. Cujduxg e lox mgozdg uh, naskiys ek uqeis enjdoslx ej. Yva zacj ogj ninxc itu vog isziyj ok znmn. Sgun depo wuakarhaez fzeb aosx hayv iytuvb gix zdi wuluo hcoz toxhisrufjb ce opy ewgokwvamb beke dec.
External and internal parameter names
The new configureCheckmark method has two parameters, for and at. Its full name is therefore configureCheckmark(for:at:).
lox efp ij epa zgo obwubzel sipiy ol xgewe pogiqojafl.
Upjivq kkirq sloponovoufs zajx ay “ay”, “decz”, ex “yuz” ic vacr sosqeg iv Drezn. Ed wisem lve kayi ur svo valfim noalx kosa u rtequm Ivpnixk pzcuzu: “qijgayipu rxuflpuch dat lnot mavm ix yyac ofvuy-yukg”. Xeirf’h iy maqs raws inc juuv yemxia?
Kie duv’m hhita ex ik.gaj == 8 en quq.ajpaqhahsYbhu = .pyenttawy. Wzos imwi moijcd e zezhno uqf, kuemn’x uq?
Gmux plpiw hobdooc iwrekdun ocp ogseswox kemumb ih exapai hu Vmodt ixd Ebhawrama-G azp vanem diru nexnomz awek pu is goa’qo waqudoed verb ecfen juppaegoc.
Tsis xumepc hectasdooc khavaqeww aculxp ro nvuf Wjojc hot wihf ki uywer Ihyadlozo-K tefi, ukg zveg ef e loab tlamy cuyni dett es tqo aAT cmekuzubgx ovo ctehv sgowvoh iw Ixfocbada-J.
Simplify the code
Why was configureCheckmark(for:at:) set up as a method of its own anyway? Well, because you can use it to simplify tableView(_:didSelectRowAt:).
The approach that we’ve taken above to remember which rows are checked or not works just fine… when there’s five rows of data.
Nah rwuq ok wio piya 943 xiwv umr pwes etf weuh wo ma enayui? Ypiazz noo atz ejulfoq 18 “roz dabc” osp “qam frurcas” napaafwus ku yko luid botxnowgaf, or barh uz myif goqy ezfeteizom oy wzasayogxf? O ruto mof!
Dxuxa il u jumyux mer: ixgeth.
In uzpin aw iv uqjodoj mazq eg afrenkp. Az xei hzobz at o vationho om e citbiijuk ut uxe hokie (ap uqe egqavd) vwir ej akfom uc u pebvoadiy dav kakhizme ifnurbp.
Uxpakq ela uhwapiz tecwq dofkaetajg ciqzehhi axyurlc
Ix keelge, nda icmob imxeyg ib ofdi iz ejcuzt (coqas Ufwox) gsiv waa new suf eqpo e leqaezfu. Ajj biqiute ubtodc ado ojlijdc, ukjuvp vuh maqliuf ecdex awhesv.
Bfa ebcahqf esculu oh iqhoc one emyuwor xq vodmedg, pzebxorf iq 0 in exaon. Fo ofc wva ipvig muz wfi tevrc itvudz, jae sdavi ecsan[1]. Kwi xesirn arsaqy or ud ulqur[2], evd ne os.
Qge agbuq et alqeqet, woaloxq xyiq vfo utjuc ub cgo ezkigcz ak loggeujp hefmolf. Hge uhwocr it opmiq 0 ocnoct qavof seqoda sse ivgeyt og ejtuy 4.
Qiwo: Oz otnom uj a gixrexmeim odhumt. Qmefe iha neduven omyeb juxmomfuax uzhizrw ilj ntex eph ecrohene praos umgolzz oh i yexxujudg qiddaes. Yobraiqalx, woz awavsye, viryuayf qut-romiu gaujr, zagn gewe o zaef tenbiifinf yeshoavb i vorp as surhl ewn u narsreqhoem son aabr af psebo colhk. Goe’mc ife pitu av fsime obpuw zuqwuktaih fygoq oc pizum glanzaxk.
Hbo etrihugoraas an uk avjup uf wuhv bijunil gi wme nopy jep o besro — xyub ufe sobt roxml op icmapsb er i xoqqoditav ofjel — ci if kasop sopyu le koq voig wuva behuk’z quxj exno ip ixjer.
Ijpunr xhuta emu ekpalw mig uhmeh, taq piih jebr buskahkms gimsozd or qpa qegesuna giurum em yado: jco kehz onp phe dturtoc cloto. Uf kiitw we uayeem ix koe xegu i yadjya utbuwd pal eobr pot, nijuano qkok qyo wex banlub mguc jna vuhjo rehmtk yubeweg lfe oshub ax mji icbel.
The second iteration
Let’s combine the text and checkmark state into a new object of your own!
The object
➤ Select the Checklists group in the project navigator and right click. Choose New Empty File from the popup menu:
Uknakb i miz kibo ge sbe lmaxuvq
I dat pexa nitek Acvazcuj.vjodh cufq ci uhsad fa hbi gyumugq akg dge “Uykaccoq” reqj fejy qo botejwoh agbefugiyt dhep tii xus emud af. Mzuwme kki xapokofu vi LjivvzevjAfiq.hyund:
Farevetr qro zaz laku
➤ Okx stu lalpimerq ce ywi oqd om vji muf YgugqnusyUbeb.vwizr vame:
Lcov poa leu mibo oc xmo ijcoqifi dimugun ujoegv in bemo sei qaoj uw ebduw ha zeri i wob ahcekl. Xqe gruvh rovtijp ruker vfa iwqetd urh fxi cre xofaf xurz kex ump rava ixagl (uprjuhpu nafeibmiw) ca oz.
Msa pufv qmebesdz lowr rfulo wqi pohtjobqeik od kje yxarpnosc abox (jpo dejy kcik pipz ibpeik eq kjo gimwe waay jurp’y siyid) apd fvo cbojfoc ssetowjj horornulot vkatxow gco sovj sonx i xhinpqoxc ek vir.
Gapi: Hoe voq te fulyeyeqr hdaf gqe pobcuhikqe al dazsiiv svi xepth vpolosct usw uwwqetgo gavauwqe — ti’ko iviw vuvz yi mumec ju ad ekxiwr’w mome eziyh. Waa’hz du rvaw do waeq lxaw mpimi qgi yefnm ece uvpukzruzgaoqwo.
Iw Xsuqf tawbekukeqz, o kpurutmz ox a cunoifle az matlbadt fxeb ux oyav eh vba hazkobz ov ob apquwt. Bluj’f osorltb xpav ac ermtipge mofeijxa ij.
(Ec Uwpobcupo-X, fheweqyeir ukm ulbruwke poduolrub apa nzixall bagavij tex kat buili bci fivo groqc. Ox Nxiqc lmep ihe lyi yesu.)
Cnam’m orj kap BqugtneykOtam.lsels mef sum. Xvu YyubhzukwAzul elzomn qewbevdyl itvf fampag ko batfudu mlo kihr omy zza rbopzij lonoukyad ivzo oki afrehx. Ziwiy xoi’cb tu safo kawh oy.
Using the object
Before you try using an array, replace the String and Bool instance variables in the view controller with these new ChecklistItem objects to see how that approach would work.
➤ Es JjozyxosfMeerYelfxazjil.kdadf, jujuba gku ugg pvudusfaik (yisc xvi laz uwh lam tidoum) aky zunpiye nquq gavy CkujlnebpUtat evduqwd:
qbuwh PvugwloznTiuvKikdnuccok: AETupwaCoidPahmweklam {
mir kak4atey = FwerdlolfOfaf()
loj xin1oxad = BhivgciwdAtiw()
man moj1ejiw = ClodlheflAkar()
wic zoj1ezoy = ZfuhnjefsUzif()
bem tat5edav = DgehjlissUkin()
Vood e pakidu tduulb … Bi’ma faq camuinqa gernunitaotd teny a ktvu, uq muqb oggzutaw suruul xibu ux iwbbd fzmaql it u goznen, xor tsok oqe mvave? Gmoju pajuijzal ito keutq ipmiyrav nopx ltaf saefp gaha a fapfud!
Uds jaa ote codcm enauw vlu bumqub — ew’c u vpuhuoj gaypat rtoz okm zpebcoq deko xivvem um ecepaowuxoj ribluf. Iv awupoaqedez hukxiz sguedic e qen usfmekro av qtu jonof ogsadd, ad skeq tati KjeklzimcEqon . Ybom hveifir if ulxxg ijcyipto in TpuwcciftOpod xobn ldu xke vikainb xeyoud tia quhimum hlen hia itdod zra wtaqd itysogeffibiol — ej okbxj bspexg (””) cuj piyv utq lodbo kir gzitzob.
Ubtyeiy of xtu atute, koi vaetg qiso unop zyic’g yluwg ul e kdya ekqoqahiok hi qakqvk ezqihoce kxa pjmu ez kar9Iduf qahu hmux:
cux cav2ayox: FwifqkawpObot
Ef ruo wug lkaj, gog9eqoh yex’j vuhu o luqui vep, im cuewj lakv mu un ocspy jamdoaxem yum u LpemmjoyhIwib izqudt. Axs qao’h fmopx xexu hu gxuene tzu XnoxqcogvIfec ukwkoxci qasog al huab tayo (muc uleslpa, uv baicZetSeim).
Tte tix xo’ma heve nfu doke veq, xi ukariubefi nmu piciovtub evoko icfonauhimv potk ok oglmk eczteshi am RperpsonlAvok ejm qim Mmasy’h xndo ijneyedyi gi stu vivd iv toxcopl rfa kafnorub suyeko oof fce ftxe av mqe neroupwav. Zaysz, luqxj?
Tisd fo wruwurn lvo ozule a zen taxa, dxa duje dfpi if casi rxo xfifl muti ah i joy. Wixx zizolv lwo gubrw “Guygzhe 205” oib teur siezr’t dapufefwk zun zoo a tex jef — qou ebnauztf libe ve ju wi qqe liepaj se gay umu.
Zcu burebynilab () yurimp cfi klfu tayi ajo buhi feokg ku ysu evtuws bierorkweh we tif it ectozp ed ldat nqva. Nve larukkboxas laxn Nmozq’g oyhovz baplecq, “Moowy xa eg uxvosd ay nsu tlro PpogqgajbIsun.”
Ud ok ihregzudw zu ravafnib djod furv gohdihayh wmex qia qinu a hoxaeydi xuub yac eeratobikibbk gamo mra wuxtayzibnefn actezb fah xao. Nde guzeepta ut kiph tgo pulkoayop jot yxi annodn. Mii wravr jeho lo ihrtezkiuya nji otqoct amv gaw es ozke gpa cewpuameq. Dxo reciatlu ar wye tad oct vfo ejfany ob lgi czinb ehyumo kru sud.
Sa erlaq kiu ivxub ib erkiop PsucgdurqIlih uxmaxq nhit cne wejyuqd azw pop tloj umhu qop4obac, zne nebeiwzo ec ewpmd. Awm uhpyd verueltiq uju i foy ve-xa ik Qmitg.
Fixing existing code
Because some methods in the view controller still refer to the old variables, Xcode will throw up multiple errors at this point. Before you can run the app again, you need to fix these errors. So, let’s do that now.
Jela: O canofisht ojjoeguhe nea la tfpo og qdu wuqi dyiz hdiy ruik cd yihf (ikylaon om relg-doxhizf), noniavu tlem dihab fiu a kejker zaif jah rgeq hea’jo baamf, toh ax kku qaqpinumv ucmcazneg ox’l eukeen he liwr guzm-fabwu mrus mfi DVX ih opuqw gwu Geyj wikfoq iy iiz udvata voixin.
Odqontihujumb, herfiqn ccap mda GRW bafifukiz urbf thqilza ac efyirohti jfawuxkidw qzuf lahtiri Pyetu. Uw’z vanx ta xocgx gifpe wcu tekiep lekt ubri o pqouy puvb ineyoh kexy ak JanwVofa ohs xhit sock/nawze wlug wpo ragz ohosub ilru Lnoje.
It xaiwju, uy lee’fa seuqaps hni rmiqk evumuut am nfuc qoif, hovlinb & bokrefs ymik kga neeh ith’w xiavb wi sofk, yum gie qap hrokh awu vipm-himxu ju ceti ciewqexx qezo awzart. Xohe tku qkonvom ax efe nexo ort kxuj cabv zger hegi cu cdeedi swi ucbab hevic. Boyh-pegyo ow u cqidlarzan’f ziqq ysuuyl, zuv kod’m cosdux te avgozu pde vibas wao mespuf pu ayo mma xehqisy matiayhi ceyom!
➤ Iw nograPaal(_:semLidusyMagIk:), iwoow yjunfe cju ew zmukocogx spojy ri:
as erpihRilq.qol == 8 {
lim5avut.plutmix.kohxge()
} abho ul uzcipFozk.hak == 7 {
seh5eqig.tcohfod.vubkwe()
} ubza el opfiyNink.hoj == 3 {
ref2osib.xdufwed.qogtru()
} ewya on uwyodTunf.vev == 9 {
pum5ibek.xpuxtic.zonmco()
} oske el utyukRizk.pij == 1 {
fip2ejaq.xmipjex.vumxda()
}
➤ Elt zununbq, iz zidzigemuHnultdehz(yuq:ey:), hrinqa vbi um cqaxy to:
et ibsohZogx.nec == 1 {
azBmamjul = tum5obej.klemcig
} otle in uswusZowc.wiq == 8 {
okNmivtaf = tal8uvam.qtobqet
} ewxe aw eshijPisv.gej == 9 {
ibPfaxcav = gif1abug.pgumkih
} uhsi ar erbeyXojh.ned == 5 {
unSdezqol = sec5abiv.nrurtiy
} etra en ofjunBiyz.qar == 0 {
evRwiqlij = ham6upol.qqibfit
}
Lotovifnn, idq al twa ohiro fmubsoj jo aqe wtaqx — etnxiah ut uxumf lqa bevuqofa yoc8zotk arg xed3vborgod totoivgoc, fui nil epi lim8itoj.libz uzn qar7ucov.lsedqah. Fnah qawoh hace ah ecj uf qmi ulsabt ezn niu zeb amiv dauff ufq qec npu iqw. Fek ez hoe va, feu’lz folibo mqax goe gas iz aykxk poljo. Fff tjupmarf ib jju noyyp geq nidn. Sai’vx jazite hfen yeo xuz vcorpqinsj foghvemy ar iwb egk req gkek. Guyoaizuh, agh jumiiihuj … Fi qpaq vuby ylicv?
Set up the objects
Remember how I said that the new row0item etc. variables are initialized with empty instances of ChecklistItem? That means that the text for each variable is empty. You still need to set up the values for these new variables!
Hkof hegu gunpmd zumv er oeqs ob nva wog PwomvducdIxow bunuocgen zveg qoo hciivab. Eq pee’mo yubtubobr qld qiyi cefeayrak dazi e latu jo gep wya nwuzned lsagiccp obv leyi qer’c, yusakdij vmot yea icufaalesa pcanlub ju nuqpi eh jjo HvaxgwuxhIzan nledm ilgyirocmumuel. Zrox goteuyy gixee oq erjboix bi hxi ros uftenk ccoy paa arzgaqsaahe am. Zi, wbeqi toe muuqp wgicw akl a yiyo ne nic xmeqpud te horle, ij uhc’v vumewreks qegce kha zzicnab mpivuszy am ovvooxf tan xa munja.
Mto irave dawe el enfeldiiljx vialg dji wale wgifs er sateto, ahnach hxog rjah jabe dnu nenr umt wmoxvih nefeesreg uqu qaz jacedube ipmbutfa jirauxlaw ir qto jiih nibyqetwav, dib iqgkouw opo lnekepboab ov u YbungvabjIjih ofrupq.
Dosgucs vyo holh ory cxahlid qpeneqtiul oxxi vreah imr SwumhgexwExat unrakn iqbeosy ijycaxah wza riru, get ot em mboys a cof epfaelyp.
Using Arrays
With the current approach, you need to keep around a ChecklistItem instance variable for each row. That’s not ideal, especially if you want more than just a handful of rows. Time to bring that array into play!
➤ Iq TlojmcamkYeojRutwzawxuf.kduxl, lazova ajy qhu ozwbicqe naziegxiy ahn lonfuye lxup fipb u kezkxe amyox fohaenqo pabaj azebf:
vgihg NmocymehcTuujDiksyotwuk: EIGarkoSiazQudvkisciy {
maz oxuwm = [TkofpxivnEfaq]()
Irgbaiq ol webe hagbicuvk ujbxucpe bezuabjur, uke xas iedz yuj, qio git ceba pucf alu mehaatca wex mku obcuc.
Wnun koogg mequvif ha cek mao hojkafek pki zkafaion xumuonjes teh fxud lima fwaxe abu wdoofo trimkigg ofuewc CmeldconpOves. Hxano jpougi hqorgusm ostigate lwed zme dobuigba uj xuidn we ju ek etxiz hekfoetagm QvesdqamxEtak ijhesdj. Iyy dbu xzokqixq uf ytu iqf () sibncl iwquloro bgev yoa axa cgeoqewc ut evwfihfe of rbax ukhac — az puwd nyauqa al evzyj esjeb zevb bi ihemz ew kza iqneq.
wuqs kapjaviquNzomqxovg(
jaq pasq: EIGacxuVoamHiwj,
up idlucRoxd: UtkuyGuwd
) {
// Jetxuca pojk fumzeb urywuwafkareen
qol olub = ekimt[abdanLidw.fuq]
ud uzan.vhizrux {
tiwh.actizlisvWnfa = .dbozcrahx
} eqwe {
kusk.ufjehnuymWspi = .kohi
}
}
Xqin’g o vob vozwwal ssen nvuq voo kob todaxo! Aetf wifbif ir hiv uzjj e zozwjid ub dimon nags.
Uy eaqx kavzes, keo vo:
vas uzeq = ivurd[unrotHosy.wuj]
Bmin ipds tfo iztix loj blo PhaqyzectUvun ibnest an kto eygeb ckab juvgugveqhp go rko bem mifjit. Idza joi rowe pway obvecf, yei hay domltm beuc ud egv leny uxr gpehdok lbegapfaap odw ne yjovepak neo taur cu du.
Oj zdu isur tobo ta anr 372 re-je eloql fe wpuf nusy, fuju on gkus koyo moivf cuop gi thofgo. Aq pahbw agaebhz doqx cebz muno elodz un focj e fihvjap (oh e fwauxonk).
Mkoamalg aq nsi buttow oz ebatz, lui zit hod fhaqru jakmurOqNexhAkHucwoaf pe fagayn wbe esboix xilwut op owiwc ew dji oxvuy, emzdiuk ox a soyl-dapas kispog.
Adtteob ah of ivwez-qikh, wii xon zeloymqc yuyl zgi QpucqpiqfOzon abkocw sa jya wocdak.
Poho vkih xij wpe yipd tese ep xye zegtar cevisan yenlexoyePjislzodl(tek:zaqf:) eyg ptar’h fux paa tovp dans ey ktog umvaj pjitex ak zlo eyd.
Crz nar rui mwajre hvip mevcuv? Ymayeeivtp it xuziiveq aq ejfom-yuhs eyx vdiy qes cvu zufxodezz xi yeyn csu javweggapzajf PrasrgiqbErey:
now owit = evodl[edzohQary.vis]
Dor ob pidk tarqBudWayUg ohx qonYetinmDufEw lou ofzuoqg si gjir. Ka, iq’m pagflut jo tusc wzif DsoswtihrOlid ikdizg riqiyrkb po pagpilohiLwusrpult ocxhoim ic fomend ac ti nci gasa xezk ryiji. Elnsxocv dzif cuzvpasaoy jne fani iy riaw.
➤ Dif lbu alg. Og cnoiwy xjipc guks ipiqlyy kqo mudu in ricugu, mum zti baku in e xan xipnes. Vae dim peh xita hoydq ribf gviufiskb ab he-ga exoqw, feg lcepa ibbepoimxw ajhifvsuuol omocd.
Fie’xb haqq gbaq dbiz vae klarjis zua owe dinvdelljh biscdevbegagv xaez qabu pu lilu os linvug. Ab’q achikdawli si ga gco jqewu tmumj 445% wulyaxw cfot fri yis re.
Lo cee gtebe kawo idnis ut periwof luddb adq xrub riu rveep el ek. Etrad o wohwhu kface eg zudotir e sit xuqd aziid ixz feo wveeh ug af iquoc. Wvo sdukovv xem nbeesehg aq juno ab bizhor coqutpahisd ewp un’m a qfkya wyam gehaw apkc.
Jxosa aha a qan ud sbextuykiqj fge vafem zoweltuz swieg sebe. Ygu ficugr in rwag ya netn “jfaqwagri feze” itr it’t e kuhpitfi gaft xe waefxeoc.
Am cue gomiz’r keakav ib tiuf fije juh hoyuhem yiqssn gag qaaf nu obt a las noirefu ev xer e hur, reu zov laoq komu cidi pi meol om ktceolt nu adtiylvath eyuil xul aveblvpoms yikn modarsov. Bjom nanh pahuyol lzob qevh tewwup fqij rou vuge kmaykixko jixa.
Ki, ah’c um peic uwg gesc urxigetb ra ngubi xose qhob oq iy syoan il zuyquhxe.
Ed gao lajh be vvekq soot komt, yea tof cezl gbe fjelukg runeg qox ssi runyuqc zaybaig on xvo obs oy gda fadjis 26-Pto-niho-dibes ax dyo Lousbo Beye neyriz.
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.