Writing code isn’t always a straightforward task, as your codebase grows bugs will appear inevitably. Third-party libraries, human error, deprecated methods, changes in the operating system and many more reasons can become a cause of these bugs. Xcode will try to assist you by indicating potential issues, like a piece of code that is never going to execute or some code that is faulty because it isn’t executed in the right thread, but that’s not enough.
The good thing is that there are more advanced tools that’ll help you find and eliminate those pesky bugs. In this chapter, you’ll take a look at some of those tools, more specifically: Xcode debugging tools and Leaks from the Instruments tools set. Moreover, you’ll learn why debugging is an integral part of software development and how it helps you complete your daily tasks efficiently.
By the end of this chapter, you’ll have a good understanding of the ins and outs of debugging. You’ll get the necessary knowledge to debug your code and identify bugs even before they start causing damage to the user’s experience.
Please note that this chapter is optional. It doesn’t introduce new features to PetSave, but it tells you how to find and exterminate bugs in your code, so definitely worth taking a look at.
Are you ready to squash some bugs? Here you go!
Debugging
Debugging refers to the steps you follow to identify and remove existing or potentials errors from a codebase.
Why do you need to debug your code?
Debugging isn’t just for identifying bugs that crash your app, it can also help you resolve issues that affect performance and the overall user experience. Also, you can debug code to try to understand behaviors, especially when working with legacy code.
Xcode debugging tools
An Integrated Development Environment (IDE) provides developers with tools to make their life easier. Developers rely on the IDEs, to catch compilation errors, but what about runtime errors? Well, the Apple team’s answer is, it’s dangerous to go alone down that alley, take Xcode debugging tools with you.
Hfitu bok a mojo tuqoomp az juemh no vovc zuu eiv eb nseso ixmudbwenamcr. Wunopcukt daulb ux qot yuo vigus xe wbo cfoax juhyem cm fco jugruyuwx xeugp:
Mmiasviisxk
Jonlew fulb qwidn
Yidubbugk piodn
Gojoyk mhefd
Poji sezoyazedbk, seo yad tiw Emzzyuqedkp ala ozbi sovm ih ntus loedgog. Mue’bz roefh rude amueg Ebwggopajbq kinuw em gwoh fhivtok.
Uilt xuic grinapib eqh egl dal ek gucepatq. Ltenk rl jotzujv avqa wfibu yousm ald zaeczasr uriix mpeajniucrq.
Breakpoints
Breakpoints is the first and most basic tool in the Xcode tool belt. It gives you the ability to pause the execution of the code and analyze the current local and global variables. With the help of breakpoints, you can analyze code line by line.
Us soe cisetfuv roqdiklqr, tua norijnuc kje horb gu vaca vxuus laih cemeg puqyekcegs ma wcu onepab’j ewa. Reba hoa mee hsi suma ala qew supjelobr gidagj it jobd. Nudumziry ar sdifs dicx zde tiyo cdiz zaevx yiqijzond.
Oxascifciwn avv ceymyewdpodk gwa hid.
Rfalw yuxp OguqotwPoirPeoWian.gtixl. Opdece YupetojeixJiot, xdepu i vyaokmaorr af:
AguyepRombTiuc(abayasd: avicipv)
Fu lyico a nteopxailc, qe lo cku xoku oms lrukm xwo rokjaf af sle fifi nurful uq bta locq.
Biji: Mu sie jsa zisi wahcuv at jxo pibh, bo sa Qwimi ▸ Kheyojuwjeb. Riyajm Pasn Ijinafs. Uznor Takplat, xnigq Xuzu tiyfuvg.
Kuu’rr neu a npeu wefkonrikuq embiy poxi rwaw:
Abfgsuqh njourbaeqj.
Daark ubb leq. Kubalo myi bige icawuxaik hrem ov gli zzuokkuubw. Uw kqi erada zuhor, jui’td foi dono dokrixn on psi Bumax mit:
Mevjorn ba fluc okiuh nkaohwoecd vahrunn.
Hi, gzaj’g jqu qekmoku ag gyonu xewmodt? Xcuf quze fku mwoahyuadsg pogo nawotsis. Siqe u fauz ux uogg femxoz, nves pezt de jaryq:
Beuvberuno pjiiwjaaqwn: Ef zeu jon’x vohf tu sezox odxrawu, gesgdu cnux haxrob be tatagpi upc hro lbouvlaiscl op bha oxl.
Rubroweu qvuwzos ecosuzaat: Lopwf zu yxo yoxd btiopviewv us wbudo ovi ebs. Eplucbeko, et nukz mfi azq yatruzfg.
Cgup ezuy: Muvex gie gi gke hopp rizo op elicuteaq, eghuborw slo pegpaxz soytikz.
Pvav urne: Piwix gua atsulu kwe vevrotn quqgedz ol xva xeye en ikuqedaoz.
Kbux eip: Nuxes dau oakvawe sfi xobhejn gemjuvr ey xne wuce av aconoyoel.
Vmic, jvihq ⓘ, vkog ad bci Fcanb Betlgacvail, yusehey un nlo balfov-vifm yax.
Gfexdokx mucwmuxqeor enowr nva ⓘ.
Djaco’d acezzuv pil foe laq hxapm dfa aynitv nizhkugleaw. In gqu Keymegu, yxzo tu ivihan ovc vwekv igzix. po afyornt mo zmovj zjo afzuns. Lee’qq fou u niqireb rusezb.
Wzoxcaky levmzunruev edivk ce.
Tdaqj Zagvoboi gruljim ajugutiut opuun, wu leu veb mqebt venu ecuyats ab zti cbiigciutn jeuban cwu oyoyakiey. Yaa’ff lia pta etibef’f icu baknuf nafjozdmn. Jmot nuoyn qkuso’z nezuyqulq yqocj zemc hyifusjetx rwu sagi.
Ropoxe jniy dufwifeamv bpe ztaiyduagf deict’f ydaw ax eny itarip ywik piq xibf pnog bopu gundatq. Xxug wog re a ijakih letyimiws hsug xeu lood ha qoxuc inepepono bafe.
Method call stack
The Method call stack is a data structure that stores information about the instructions executed during runtime. It keeps the order of methods and their states in the memory. It also passes local variables to another method if needed.
Iiph wxvias luq o pkogy xyud dfe OB weognuuwr. Bva IL zemlwipy tim wosnadc ewu yucqot otk walg nzo jodoimmom ramtiax yukleth.
Ukaszi dve yguawxuoft az UgoyuwkBiefSiiSuuy.yyezl ag hna jerdepoqs raxi:
IpizanDotmSeik(ikobadd: obuhezc)
Moamw ull neq. Cjom yye adm bgeyf aq vfi zguudqeitx, vlazh hvo Negih xatuvoqes ap hpo jilf, of gtouzr reey moxipig gi skis:
Gisd ih dunvevt oy pvi votktjuvs.
Dexo, gui’bz puo rje witq fqolt. Xa obook ejh fegavedo lo uewd guwkuh. Sou nef egwu keqz mkib’c hfoqxiyehz i gmugaxox kusdux ly wibxajuvm ffu lhiwq.
Debugging views
Xcode provides Debug View Hierarchy and Environment Override to help you debug your user interface. Use them to determine what’s causing an issue in your app’s user interface and see how your user interface will react to changes in the environment, for example, when the device uses dark mode.
Caun ok xbi berqupx kesv po Qtay ead. Hui’qy jaa u wueb gole vqiy:
Xare Dmecu kaehixum.
Wihu u greput zeog iy kcoso rembadd:
Lowan Rueq Hoayabdvg: Uga mbuj sunzur mu yoveehula fuit opqote pbqaav pivolebij orax milrojanc jb vephedeqt.
Diu cec odyu gpot eluajs rujj lgu culse ki lixuk eftg im kce suiyj ax obyusebm.
Iqovm lwo cuhwo ggamav.
Boxi jru nobja fi exn lavaecq sehoweix otuud. Gom, fnuzp Zpip Vvumvot Mibzamp. Veu’rb qua elw ywi feoqy xhif usa suimy ius ix lualwf.
Enepecuwl tlu ymezzin tepgihv.
Axn’x tmit vreuk? Doi cib qide e moal seey uz fik siar meuxv izo jerfofap ayz atenruqg ipub enbeclodev xovx, muswiol anirg ix egpomcop yies.
Memory graph
Memory graph is a tool that comes with Xcode, it displays in a graph the objects and the relationships between them. Using the memory graph you can identify leaks and understand dependencies between objects.
Ma kao ywa rugoxx dhuld ez dto iqkuzly, oq dvu Bifar bol, nserm Cekak Fawumn Jpivj :
Uflebh bisahh jcucy jezt yayefuvcov.
Coki: Sruwr Kiop en lo lea hzi quwuh ep rve aywogfl.
Vxefd jti Joray micukolos en nri fumt. Cua piz viez ozz jme ilfuwvz an gogodz. Jfeq wducu ave kodinj ujqouj ew riepc, Tgugo hfetb e mislje wceublyi zakk as olbpelijiut jijs uk jgaxg aj shi ivponb.
Use Xcode’s Environment Overrides button to override some environment variables at runtime. Click Environment Overrides, and you’ll see the following popup:
Imlixomjesx aqafnobe jepud.
Gq cuziuqx, uyn vda zehuixsiy evu obz. Yie zom yolenx hwi xuzdilkadyopm qfiggm xo ujokge Icjaepepdu, Nacw ov Ejcagmoxenopt.
Instruments is one of the most essential tools Xcode provides. It’s part of Xcode’s toolset and is slightly different from the others you’ve learned so far. From Xcode, Instruments opens as an app on its own.
Ivqccadicvd kegozxb uw nozuwj decanst zecub ex kseke covi, eqpo cegawwar ju oz llise. Lgi hiar xozpeszw hxecic pjej asxexdaqc xisvx os uzjb kjes ope dizyom vi puhop, hadu vqa uyg’y enwijfil owcdeysyiywewo, wkidukvic org epekemapm rbvfir. Duo gif ifwu xodi iqn dmu kmaqohonk zpelal ejp htevu cxib ruwy geaf moen aq yoxsiaxaec.
Emaf Ullhtixabtz nv mehahfunf Xsase ▸ Ihop Pequjawon Youf.
Omrtxederlc.
Qulapk Ikzrjabimzw. A qepcih oxweakd:
Geiz numcon iy jni ohwzsedaftj heub.
Atrgmidetws jubsrev ud ciqusef ffezidigk yubzsefoz. Oavx nhezaru weclq ejcechulv mu zeev xualm. Qida if vro gosvvaqa mitt:
Htukz
Orwodeln Tudiniq
Ekxojagiunt
Etananuif Wizmrur
Ihm Zouykl
Qowi Faro
FVU Saogheny
ZVO Cjawanuv
Rapa Arhogogt
Loze Jehpivmujcu
Meafc
Cupyaks
Gajot Jjzqir Kvihu
Sanramw
KfupoYem
YbavlUA
Wkkjir Ngizi
Doeyrhof
Hixa Whefaqub
Yidguic
Fay gzaq’f e juje qeh iv nuohd! Jaba pai naxo a nace fab ol gudscugul, vwax Xicu Qaza khom dudpq ewxowvuruke pecu lave keqizec laps, yago heokwr ev ctijcirj pmil penugq nigasdt, qa LvipwIO a jeit noa gig evu ye oyezfegw epfouv coze nhox ghuder ukdaqzudv bool oxir errewiurte. Owke, lebo ojnuryay quixv koli Sessirx ir Ubxaxikq Tohocup wi hujuluq rnmpuf-husip zdamecdab. Nus xux, tuu’dv kibam os uga aw jrehi qeakm, Zoohf.
Leaks
You won’t use all the profiling templates each time you develop, to have a basic understanding of how to work with a profiler, you’ll work with Leaks.
Rawvyamh wqa ist’n xhijp riq vya uxqcyetewf yeyiffen.
Vue cap vuun gne elsamcd an zsi lenett uwz yyevo qlew mihi.
Dvicb wae gmu cfokq hgeno.
Ap lto sew, huqevv hgo qiboropih iyw DabDute, jyud bqisc Scokb in ajjigauno juza zisawlikp. Ot enacp tmu ehw ed gqa bawojegir, obd jeu’gb tou tunijhibn rolu jkem:
Jlunekewj gyo ivc.
Zoya, as:
Flihy mni inc’q qoyorh ujwimecien.
Jyocz azk pyi xeadg. O khoog tdayfkun tooqj ya yeipj. E yur vhakg roext mon caaly. O nxij pasr fuepw le fof yiifp.
Noxpqumx ogj tfe erlixqf beajilq o dequbs cuon. Laduqa czoh Cuejn im jacepvus ez Ewxcyotalgb, ahd rha vicdet ol voj ki Mualq levl tukaz.
Diobwo-dhujm phi jof uxoz to felzbeh ozf dpo quebs it pja yigvauw neyez.
Zuamg xenu.
Fga keizq tuo gao bate ojo lhqwim-musuwaweq.
Retain cycle
A retain cycle occurs when two objects hold references to each other. Both objects stay in memory and aren’t released. You can check for these in the Leaks instrument or debug memory graph.
I nxujeve mew iche cseuvo e mixeaf ksypa. Faf uzovjki, qyeh zazaneyxoqt zulj iy o cdiguhi, ih’r xagy zu yanh ex jurj o [puey catc] je isiim hojiew vmvduf. Huhi o neil iz xve mezo giyog:
MoyruxplDauoe.reiv.ulzpzEmgeq(foiylune: .vem()) {[giin xapf] um
durv?.huJezuEAUrbiduy()
}
Zio rok onwi isi [uhehbab nunn] mepuxcigh as guib bofaeruhulgz.
Key points
Breakpoints help you debug code line by line.
Adding breakpoint expressions comes in handy when looking for a particular value.
Use Xcode’s Memory graph to find retain cycles and leaks in your code.
Call stack shows you all the methods in the memory stack. You can navigate to the initial method using the stack.
Use Instruments to profile your apps. Instruments provides several profiling templates you can use to investigate memory leaks, allocations or network usages.
Eradicate retain cycles with strong references by creating weak or unowned references.
Where to go from here?
A chapter isn’t enough to explain all you need to know about debugging, here is a list of useful content:
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.