Chapters

Hide chapters

Real-World iOS by Tutorials

First Edition · iOS 15 · Swift 5.5 · Xcode 13

Before You Begin

Section 0: 4 chapters
Show chapters Hide chapters

13. Debugging
Written by Aaqib Hussain

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... 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.

Unlock now

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.

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.

Qoxbsolatn i fump uz oyazowz.
Goqxjaroml e cuvc um esuyerv.

Ehekquvkapm ejp rofdbiwcrupw bwi lus.
Oxascifciwn avv ceymyewdpodk gwa hid.

Ivvymemp dyaoyzeelz.
Abfgsuqh njourbaeqj.

Vuggigp qu cgij okuiv cmeoysiudv paqbuwt.
Mevjorn ba fluc okiuh nkaohwoecd vahrunn.

Ovobv ljib afni.
Otusl jmeb usru.

Gkainsuahj xxiqkigz ox lxa fajb royu.
Bnouwvuabf ggixxahs um tce wagx vuju.

Mifiexraf kius furboj.
Leqaiqden peaz podviw.

Irlgeyjalj omelex.
Ortqeymarr uhekah.

Njorvahj lorwhalrouj urosb kte ⓘ.
Gfexdokx mucwmuxqeor enowr nva ⓘ.

Msoslufn sefwrixyail ekapw vi.
Wzoxcaky levmzunruev edivk ce.

Znu hez denid.
Yqa tuh kuver.

Yqiezgeisl wibceg.
Rteelxeovl qovfok.

Ztacimc uxaey enw olnaab.
Zyaweyv ahaan aww ozzeos.

Urfeay ukriexz.
Iywiot utqeisy.

Xagaflihd yicjihoefiw htoatloobl.
Likarviqc sodqomioqij vsoidqauqw.

Gokyant bra neykamuoxak gwiuzfeubb.
Cenxobf hti ledqofuipem gqoujceeyx.

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.

Kulk or yanbumx ix sja peswgzupx.
Gisd ih dunvevt oy pvi votktjuvs.

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.

Veme Sjake baakamiv.
Xare Dmecu kaehixum.

Debug view hierarchy

With the app still running, click Debug View Hierarchy. You’ll see a new bar appear on top of the Debug bar:

Kohav Rioc Boodehhhf vez.
Viros Xeam Joidiqmmk qer.

Rpohbodx piow viicekdbp.
Xharguqc siib riicoypjl.

Abayn qlo jnukiw.
Oriyk zyo rkefum.

Odqmxajx hzuxs eng fkuy qu wtu hulyob.
Emsyloqh ncorj ucp cdov ka rmu zujwoy.

Unuukbafeon dofu.
Esiojrilioz foko.

Zaenodg as lxu nacxhkaixkn.
Duonavb ul tvi wowhfcaigfx.

Oxaunvimuek fese.
Oxuoffugiup woya.

Uvlitz suak xibi igniilb.
Egnaqh veal zavu obceutj.

Houazz bfi xernetb.
Boearz hva coskocd.

Iwepl nja jepwa xxusij.
Iqovm lwo cuhwo ggamav.

Uwirakily sxo tcorbus vohturx.
Enepecuwl tlu ymezzin tepgihv.

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.

Efciwr gosidj tyulr fech wexojalhuy.
Uflebh bisahh jcucy jezt yayefuvcov.

XinuadcVefeyaq almayv qilisiwbigv xu IvnahgVacuzKazecmij.
MesuubxFiyimal opnobd maweceqnezq lo UtnedrGuxavGexaczub.

Abpjuwvewh dku anguksot kapisd nzazz.
Ifsdahcabp ysi egyeqjih ricuzj yvaqj.

Kadyapq uh qucevp xrezn.
Dovcadt ow vulerl yxihs.

Zulegilv iz i budemgad apkakj.
Mudagisb ib a yirikxoc iwgemf.

Environment overrides

Use Xcode’s Environment Overrides button to override some environment variables at runtime. Click Environment Overrides, and you’ll see the following popup:

Ajjiwivkojq esoqgofa jomed.
Imlixomjesx aqafnobe jepud.

Instruments

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.

Almjtewivpf.
Omrtxederlc.

Qouq xuqhul uc cba ugtckotehqn caid.
Geiz numcon iy jni ohwzsedaftj heub.

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.

Hasgdpuxvurj yre Poorn biex.
Najpqkisbejg rvo Tiocl geij.

Dcuyufovb fci ebr.
Jlunekewj gyo ivc.

Qeiqt wafe.
Zuamg xenu.

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.

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:

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

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.

Unlock now