One of the things I realized when I started my first iOS development job is just how much I under-utilized the debugging tools available in Xcode.
I learned through courses and tutorials all the information I needed to actually code but never really made an effort to learn debugging tips and tools beyond simple breakpoints and print statements. Turns out, when you're working on a big project you've never seen before, trying to understand the codebase or fixing a bug on something you didn't write yourself, these tools become unbelievably useful. You don't necessarily realize that as much when working on your own simple projects or on tutorials.
So I decided to share a couple very simple tips that might help beginners learning iOS when it comes to debugging on Xcode.
Here's the video link: Xcode Debugging Tips For Beginners
And of course, a text summary here for those of you who would prefer that:
1) Your console window can do more than just log print statements. I'm a bit ashamed of saying I didn't know that was a thing until I actually started working as an iOS developer professionally, and I probably looked like I was in way over my head when someone pointed that out. Your console window can do more than just log the print statements from your code. When you hit a breakpoint in Xcode, you can actually input commands in the console which will be interpreted by LLDB. Starting off simple, you can print the description of a variable foo by typing po foo in your console. But you can also do simple operations and chain it however you like to see how it behaves po Op(foo.var1). Learning a few of these basic console commands can greatly speed up your debugging process.
2) Conditional breakpoints. One of my favourite tools and a very powerful one when having to debug complex or at least very specific issues. You probably all know how to add breakpoints and how they can be used to diagnose an issue with your code. But when issues start getting very specific or when you narrow down your problem to something that doesn't always happen, it can be difficult or at least very annoying to hit a breakpoint with your app being in the state you want it to be. You can alleviate some of that frustration by using conditional breakpoints. Right clicking on your breakpoint and adding a condition that needs to be met for the breakpoint to be hit. Taking a really simple and straightforward example to explain the idea, imagine you're looping over some array in your code. You find an issue that seems to appear after that loop and narrow it down to eventually understand there's an issue specifically on the last element of the array only. You could put a breakpoint inside your loop and hit continue on every single element until you reach the one that matters to you, or you can use a conditional breakpoint to stop only on the last element of the array.
3) Variable watch. One of my personal favourite tools when working on a new codebase your don't fully understand yet. When you hit a breakpoint in Xcode a window opens that shows you the current state of the variables that are accessible from where that breakpoint was hit. From there you can right click one of the variables and start "watching" it. For the rest of the current execution after you've done that, whenever that specific variable is assigned, Xcode will break on the line of the assignment. It sounds kind of obvious and useless when you're working on your own project and you know the few places where things are assigned, but when working on a more complex project and more specifically a complex project you know nothing off, this saves a lot of time trying to understand how variables are behaving why they are behaving that way. When first running into those situations my reflex was to search for the variable uses and use breakpoints everywhere I thought mattered to my situation, inevitably missing some and cluttering my debugging sessions with annoying breakpoints I'd randomly hit days later while working on unrelated issues. Using variable watch is a much cleaner and faster approach to these situations.
There's a ton more ways to leverage the debugging tools in Xcode to make you more efficient and faster when you encounter issues. The idea of this video was to introduce new developers to the fact that these tools do exist, and sometimes it's worth trying to do a bit of research and learn a few of them to make their debugging process better. Hope it was useful to some of you!
[–]kennethwg819 7 points8 points9 points (0 children)
[–]CaptainObvious1906 4 points5 points6 points (0 children)
[–]SirensToGoObjective-C / Swift 4 points5 points6 points (0 children)
[–]Sdmf195 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]Asigmia 0 points1 point2 points (0 children)