Debugging in Xcode (continue)
My first post will walk you through Debugging in LLDB
******Debugging for UI issues related to Localization******
It is almost certain that the strings lengths will vary in every language. So it’s very important to make sure that your app looks right for every language.
A very handy way to doing so is -
Click on your project name -> “Edit Scheme” This will open up the scheme’s page
Select “Run” Debug configuration -> “Options” -> “Application Language” -> Pick the language of choice -> “Close”
Congrats! By doing the above steps, you just successfully converted you simulator to run in chosen language.
Now let’s take 1 step further in improving our lives. Xcode enables us to create duplicate schemes and we’ll make use of this ability to simplify our debugging efforts for localization further.
This time click on project name -> “Manage Schemes”. This will bring up the list of schemes we might have for our app.
Select the original scheme -> click on settings button at the bottom -> select “Duplicate”. This will duplicate the scheme for you - meaning, it’ll bring up the edit scheme window.
In the Edit scheme window, you can rename the scheme name to “ProjectName LanguageOfChoice” for “Run Debug” config. Change the “Application Language to the language of your choice and hit close. This will create another scheme with the appropriate name.
You can reset the original scheme to reflect system language and save.
Now when you click on project name, it’ll give you choice of running the app in original scheme or in the language of your choice (hmmmmm...... how convenient and handy it is). We just made our debugging life a bit simpler :)
In addition to above, if you edit your scheme, you’ll have several language options but keep scrolling down to explore further
Some of the options like - “Double-Length Pseudolanguage” will double your strings and you could see long text in your language to see if it broke the UI. You can also choose “Right-to-Left Pseudolanguage” which will basically reverse the language running from right to left (for certain languages like Arabic). In those cases, you can adjust your views constraints to make sure the views still look right. This is AWESOME!!!
******Main Thread Checker******
Sometimes, by mistake we may update UI on background thread. The way we can make sure to catch it is very SIMPLE :)
Click on project name -> “Edit scheme” -> “Run Debug” -> “Diagnostics” -> make sure to click “Pause on issues”. “Main Thread Checker” by default should be ON. If not turn it ON. and then close it
This will pause on the line which may be updating the UI on background thread. This way Xcode makes it very convenient for developers to catch such mistakes.
******Malloc Stack Logging******
Mallock stack logging is a very useful tool to track if we are leaking memory. Only enable this setting when you are debugging the memory leaks issues for suspicious leaking objects. The way we can turn this ON is going to “Edit schemes -> “Run Debug” -> “Diagnostics” -> “Logging” -> “Malloc Stack” -> “All allocation and Free History”.
By turning this setting ON and debugging potential memory issues on ViewController (for eg.)
Build and run fresh after making the setting.
Tap on “Debug memory Graph” button just next to “Debug View Hierarchy” button to start debugging for memory leaks.
The Left pane will look like below. Look for the number of instances of your view controller shown in brackets on side of their names.
As you can see in the above picture, for me “RestaurantsTableViewController” has 2 instances.
Click on the RestaurantsTableViewController and select the instances one by one. You should be able to see on the right pane (make sure to select the “Debug Memory Graph” tool) the stack trace of what lead to creation of double instances of RestaurantsTableVC. As you can see, you can click to the little arrow on the side of the method which will directly take you to the exact spot of the code where the memory allocation for this VC is happening.
This is how you can discover if you are creating retain cycles or initializing the VC multiple times and so on...
******Use unsafeBitCast to view properties of objects in the debug memory graph******
Sometimes, when we are debugging issues with memory leaks, we have a need to check for properties of the object to come to understand what is making things go south? Follow the below steps:
First set a break point of where you possibly will have the object under investigation is well initialized and will have all the states fully defined. Run your app and interact with the app to trigger the break point.
In my case, I wanted to debug a cell’s properties like its imageView and cuisine title label. So I put a breakpoint in didSelectCellForRowAtIndexPath(). I triggered my breakpoint by selecting a cell. So remember, it is important to have a debug session going before we can do anything.
Now click on “Debug Memory Graph” button
Click on any cell of your interest and copy the memory address like so
And type in debug console
p let $cell = unsafeBitCast(0x7fd65e0fec00, to: RestaurantCell.self)
hit enter
then on the left pane in the variables window, right click and choose “Add Expressoin”
Enter the property that you are looking to debug.
In my case, I’m looking for cuisineTypeLabel property and coverImageView property. So I added both of them.
In the Expression textfield - $cell.coverImageView.image
and similarly add another expression - $cell.cuisineTypeLabel.text
Once you’ve added the properties you are watching for you can select them and click on the eye symbol at the bottom of the left pane to view its value in a popup like so
Isn’t Xcode Awesome!!! Love it..












