<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Vishal Deep</title>
    <description>The latest articles on Forem by Vishal Deep (@vdeep).</description>
    <link>https://forem.com/vdeep</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F277633%2Fa630a600-85b6-403b-a338-bc0e6f2d3a6c.png</url>
      <title>Forem: Vishal Deep</title>
      <link>https://forem.com/vdeep</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/vdeep"/>
    <language>en</language>
    <item>
      <title>iOS Debugging Tips</title>
      <dc:creator>Vishal Deep</dc:creator>
      <pubDate>Tue, 17 Jan 2023 14:25:37 +0000</pubDate>
      <link>https://forem.com/vdeep/ios-debugging-tips-2m33</link>
      <guid>https://forem.com/vdeep/ios-debugging-tips-2m33</guid>
      <description>&lt;p&gt;Collection of some iOS Debugging tips.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Changing variable values
&lt;/h3&gt;

&lt;p&gt;Add a breakpoint where you want to change the variable value. Then use the following command (lldb) to update the variable's value.&lt;br&gt;
In this example, I am updating the value of &lt;code&gt;address&lt;/code&gt; variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;expression address = "New address"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use &lt;code&gt;expr&lt;/code&gt; or &lt;code&gt;ex&lt;/code&gt; (short forms) instead of &lt;code&gt;expression&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Printing variable value
&lt;/h3&gt;

&lt;p&gt;When the execution stops at a breakpoint, you can use one of the following to print the value of a variable to the console:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;p&lt;/code&gt; (a.k.a &lt;code&gt;expr --&lt;/code&gt;) - This prints the value of the given variable.&lt;br&gt;
Usage - &lt;code&gt;p address&lt;/code&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;po&lt;/code&gt; (a.k.a &lt;code&gt;expr -O --&lt;/code&gt;) - This also works as &lt;code&gt;p&lt;/code&gt;, but if the passed variable is an object, it prints the output of the &lt;code&gt;description&lt;/code&gt; for that object.&lt;br&gt;
Usage - &lt;code&gt;po address&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  3. Printing values of non-computed variables
&lt;/h3&gt;

&lt;p&gt;Use &lt;code&gt;v&lt;/code&gt; instead of &lt;code&gt;p&lt;/code&gt; or &lt;code&gt;po&lt;/code&gt; to print the value of non-computed variables (e.g. IndexPath).&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Skipping lines of code to execute (Moving the instruction pointer)
&lt;/h3&gt;

&lt;p&gt;While debugging, you can move the instruction pointer to the desired location to start execution from that line.&lt;/p&gt;

&lt;p&gt;Or, you can use this lldb command to jump by n number of lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;thread jump --by 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Printing view tree (recursive descriptor)
&lt;/h3&gt;

&lt;p&gt;Using this lldb command to print the view hierarchy for a particular view:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;expression -l objc -O -- [view recursiveDescription]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. Adding aliases
&lt;/h3&gt;

&lt;p&gt;To create an alias for an lldb command that you use often, you can create an alias like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;command alias poc expression -l objc -O --&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, instead of typing &lt;code&gt;expression -l objc -O -- [view recursiveDescription]&lt;/code&gt;, you can simply use &lt;code&gt;poc [view recursiveDescription]&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Printing object description using a memory address
&lt;/h3&gt;

&lt;p&gt;Use this lldb command to print the description of a memory address.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;expression -l objc -O -- 0x7fc2b240e3a0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or using the alias we created earlier:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;poc 0x7fc2b240e3a0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  8. Returning early from a function
&lt;/h3&gt;

&lt;p&gt;Use the following lldb command to return early:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;thread return
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  9. Changing UIView properties on the fly
&lt;/h3&gt;

&lt;p&gt;To change the property of a UIView, use this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;po unsafeBitCast(0x7fc2b240e3a0, to: UILabel.self).text = "New text"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Followed by:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;expression CATransaction.flush()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  10. Adding a fancy description for your own classes
&lt;/h3&gt;

&lt;p&gt;To print a different output to the console when an instance of your classes is printed, you can use &lt;code&gt;CustomDebugStringConvertable&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;YourClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CustomDebugStringConvertable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;debugDescription&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"Your custom description"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>watercooler</category>
      <category>music</category>
    </item>
  </channel>
</rss>
