<?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>DEV Community: Alex Figueroa</title>
    <description>The latest articles on DEV Community by Alex Figueroa (@externconst).</description>
    <link>https://dev.to/externconst</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%2F14843%2Fa8dba556-400b-47db-a61e-fa88f668b82f.jpg</url>
      <title>DEV Community: Alex Figueroa</title>
      <link>https://dev.to/externconst</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/externconst"/>
    <language>en</language>
    <item>
      <title>Unexpected Closure</title>
      <dc:creator>Alex Figueroa</dc:creator>
      <pubDate>Fri, 05 Oct 2018 22:15:51 +0000</pubDate>
      <link>https://dev.to/externconst/unexpected-closure-1j94</link>
      <guid>https://dev.to/externconst/unexpected-closure-1j94</guid>
      <description>&lt;p&gt;Closures in Swift are a great feature and are useful for tasks such as network callbacks, notification subscription, and providing an alternative to the delegate pattern.&lt;/p&gt;

&lt;p&gt;Recently, I discovered that code as inconspicuous as a &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; (and) conditional could also leverage this feature.&lt;/p&gt;

&lt;p&gt;Let's assume that we are modelling a &lt;code&gt;User&lt;/code&gt; of a global subscription-based application where users can customize their theme color.&lt;/p&gt;

&lt;p&gt;We could represent this in Swift as a struct that has properties for its: identifier, subscription status, country code, and theme color.&lt;br&gt;
The resulting &lt;code&gt;User&lt;/code&gt; model could look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;User&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;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;isSubscribed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;themeColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIColor&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;countryCode&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We'll make the &lt;code&gt;themeColor&lt;/code&gt; optional here since it'll be up to the User if they want to customize this.&lt;/p&gt;

&lt;p&gt;Assume that our application already had an object to represent a theme called &lt;code&gt;Theme&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Theme&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;backgroundColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIColor&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;backgroundColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIColor&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;backgroundColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;backgroundColor&lt;/span&gt; &lt;span class="p"&gt;??&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;white&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;



&lt;p&gt;Observe that this &lt;code&gt;init&lt;/code&gt; will fallback to &lt;code&gt;UIColor.white&lt;/code&gt; (or &lt;code&gt;.white&lt;/code&gt;) if the &lt;code&gt;backgroundColor&lt;/code&gt; property is &lt;strong&gt;not&lt;/strong&gt; provided.&lt;/p&gt;

&lt;p&gt;Given this knowledge, we could create a new class just for the custom User theme.&lt;br&gt;
We can initialize the custom theme with a User in order to grab its &lt;code&gt;themeColor&lt;/code&gt;. The theme color passes to its parent class as its &lt;code&gt;backgroundColor&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;Theme&lt;/code&gt; subclass can represent the custom User theme object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;UserTheme&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Theme&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;backgroundColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;themeColor&lt;/span&gt;&lt;span class="p"&gt;)&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;



&lt;p&gt;Let's assume that we have received additional requirements that limit User theming to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;subscription users&lt;/li&gt;
&lt;li&gt;Canadian users&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We could then update our &lt;code&gt;init&lt;/code&gt; method on &lt;code&gt;UserTheme&lt;/code&gt; to handle these cases as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;themeColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIColor&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isSubscribed&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;countryCode&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"CA"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;themeColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;themeColor&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;backgroundColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;themeColor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Although this looks alright, it'll actually fail to compile. You'll see the following error log in Xcode:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error: 'self' captured by a closure before all members were initialized
        if self.user.isSubscribed &amp;amp;&amp;amp; self.user.countryCode == "CA" {
                                     ^
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This error message while informative is a bit confusing. What closure is it referencing?&lt;/p&gt;

&lt;p&gt;We could easily fix by removing the explicit &lt;code&gt;self&lt;/code&gt; and instead rely on the &lt;code&gt;user&lt;/code&gt; property passed in as a parameter. This fixes the symptom and not the root cause which is that there is an implicit closure in this line of code?&lt;/p&gt;

&lt;p&gt;After posting this error message to the &lt;a href="https://www.meetup.com/tacow-org/"&gt;tacow&lt;/a&gt; Slack group, &lt;a href="https://twitter.com/rydermackay"&gt;@rydermackay&lt;/a&gt; pointed out to me that the Swift language is capturing the right-hand side of the conditional in a closure.&lt;/p&gt;

&lt;p&gt;That is, given the conditional: &lt;code&gt;lhs &amp;amp;&amp;amp; rhs&lt;/code&gt; ("lhs" and "rhs" represent Left-Hand Side and Right-Hand Side respectively). &lt;code&gt;rhs&lt;/code&gt; is being wrapped in a closure. More specifically, it is wrapped in an &lt;code&gt;autoclosure&lt;/code&gt; so that it could lazily evaluate the right condition if the left condition was false.&lt;/p&gt;

&lt;p&gt;This can be shown by looking at the source code for the &lt;a href="https://github.com/apple/swift/blob/7f105e4e3a994e6ac87860d5bd7bf9942c52b4bb/stdlib/public/core/Bool.swift#L289"&gt;&amp;amp;&amp;amp; operator&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;@autoclosure&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;rethrows&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lhs&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="nf"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In order to understand the warning a little better, we need to know what an &lt;code&gt;autoclosure&lt;/code&gt; is.&lt;/p&gt;

&lt;p&gt;From the Apple documentation, an &lt;code&gt;autoclosure&lt;/code&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;...is a closure that is automatically created to wrap an expression that’s being passed as an argument to a function. It doesn’t take any arguments, and when it’s called, it returns the value of the expression that’s wrapped inside of it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;but most importantly:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An autoclosure lets you delay evaluation because the code inside isn’t run until you call the closure. Delaying evaluation is useful for code that has side effects or is computationally expensive because it lets you control when that code is evaluated.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That means that the above &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; implementation at a high level is equivalent to the following. Note: We can't actually write &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; without the &lt;code&gt;rhs&lt;/code&gt; since it's defined to have both parameters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// For simplicity, I've removed all throws&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lhs&lt;/span&gt; &lt;span class="p"&gt;??&lt;/span&gt; &lt;span class="nf"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Example: Assume A and B are some boolean conditions&lt;/span&gt;
&lt;span class="kt"&gt;A&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;B&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we update our previous &lt;code&gt;UserTheme&lt;/code&gt; &lt;code&gt;init&lt;/code&gt; with this re-interpretation, it would look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isSubscribed&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;countryCode&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"CA"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;themeColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;themeColor&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see, the block captures &lt;code&gt;self.user.countryCode == "CA"&lt;/code&gt; and since we're doing this before &lt;code&gt;super.init()&lt;/code&gt; when all members have initialized the compilation fails.&lt;/p&gt;

&lt;p&gt;There are a few ways to fix this, we could:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;move all this offending code to below the &lt;code&gt;super.init()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;store the result of the right-hand side expression in a separate variable, or;&lt;/li&gt;
&lt;li&gt;we could move the creation of Themes to a &lt;a href="https://en.wikipedia.org/wiki/Factory_method_pattern"&gt;factory&lt;/a&gt; class to avoid a &lt;code&gt;Theme&lt;/code&gt; subclass&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Either way, these all work but in general you should typically avoid referencing &lt;code&gt;self&lt;/code&gt; before the &lt;code&gt;super.init()&lt;/code&gt; in cases like these.&lt;br&gt;
I hope you learned something and potentially got you interested in looking at more implementation details of the Swift programming language.&lt;/p&gt;

&lt;p&gt;The sample code can be found &lt;a href="https://github.com/ajfigueroa/blog-code/tree/master/posts/3-Unexpected-Closure.playground"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Resouces
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Closures.html#ID543"&gt;Autoclosure Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://krakendev.io/blog/hipster-swift#autoclosure"&gt;Hipster Swift post by KrakenDev&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/apple/swift"&gt;Swift Repo&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Originally posted on my &lt;a href="https://alexanderfigueroa.com"&gt;website&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
    </item>
    <item>
      <title>DFSubviews: DFS and UIKit</title>
      <dc:creator>Alex Figueroa</dc:creator>
      <pubDate>Sun, 07 Jan 2018 22:40:46 +0000</pubDate>
      <link>https://dev.to/externconst/dfsubviews-5182</link>
      <guid>https://dev.to/externconst/dfsubviews-5182</guid>
      <description>&lt;p&gt;If you've studied Computer Science or prepared for technical interviews then you've likely seen &lt;a href="https://en.wikipedia.org/wiki/Graph_traversal" rel="noopener noreferrer"&gt;graph traversal&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The two popular traversal algorithms are: &lt;a href="https://en.wikipedia.org/wiki/Depth-first_search" rel="noopener noreferrer"&gt;Depth First Search (DFS)&lt;/a&gt; and &lt;a href="https://en.wikipedia.org/wiki/Breadth-first_search" rel="noopener noreferrer"&gt;Breadth First Search (BFS)&lt;/a&gt;. Both of which have lots of &lt;a href="https://en.wikipedia.org/wiki/Graph_traversal#Applications" rel="noopener noreferrer"&gt;applications&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I believe that UIView's private function: &lt;code&gt;recursiveDescription&lt;/code&gt; is an application of DFS and I'll attempt to re-create it with that assumption.&lt;/p&gt;

&lt;h2&gt;
  
  
  Assumptions
&lt;/h2&gt;

&lt;p&gt;I'm going to assume you're familiar with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;how the &lt;a href="https://developer.apple.com/library/content/documentation/General/Conceptual/Devpedia-CocoaApp/View%20Hierarchy.html" rel="noopener noreferrer"&gt;view hierarchy&lt;/a&gt; works in iOS; and&lt;/li&gt;
&lt;li&gt;Swift but it's not necessary assuming you're familiar with another language

&lt;ul&gt;
&lt;li&gt;There are some Swift only features that I'll do my best to explain.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;I'm going to walk through a brief recap on Graphs, Adjacency Lists, and DFS. Afterwards, I'll use this as the foundation for reverse engineering &lt;code&gt;recursiveDescription&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Graphs 101
&lt;/h2&gt;

&lt;p&gt;Let's first look at the following graph:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Figure 1. Directed Graph&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ff043vm2qhzufdgkezj3e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ff043vm2qhzufdgkezj3e.png" width="800" height="608"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a &lt;strong&gt;directed&lt;/strong&gt; graph that consists of six (6) &lt;strong&gt;vertices&lt;/strong&gt; and five (5) &lt;strong&gt;edges&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Vertices&lt;/strong&gt; (a.k.a nodes or points) are the encircled data

&lt;ul&gt;
&lt;li&gt;Ex. The above graph has vertices: &lt;code&gt;A&lt;/code&gt;, &lt;code&gt;B&lt;/code&gt;, &lt;code&gt;C&lt;/code&gt;, &lt;code&gt;D&lt;/code&gt;, &lt;code&gt;E&lt;/code&gt;, and &lt;code&gt;F&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Edges&lt;/strong&gt; (a.k.a. arcs or lines) are the lines that connect each vertex

&lt;ul&gt;
&lt;li&gt;They can also have &lt;a href="https://en.wikipedia.org/wiki/Glossary_of_graph_theory_terms#weight" rel="noopener noreferrer"&gt;weights&lt;/a&gt; associated with them&lt;/li&gt;
&lt;li&gt;Ex. &lt;code&gt;A&lt;/code&gt; has two edges associated with it: one to &lt;code&gt;B&lt;/code&gt; and another to &lt;code&gt;C&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Directed&lt;/strong&gt; refers to the &lt;a href="https://en.wikipedia.org/wiki/Orientation_(graph_theory)" rel="noopener noreferrer"&gt;orientation&lt;/a&gt; of the graph 

&lt;ul&gt;
&lt;li&gt;This specifically refers to &lt;strong&gt;how&lt;/strong&gt; edges connect each vertex&lt;/li&gt;
&lt;li&gt;A graph can either be &lt;a href="https://en.wikipedia.org/wiki/Directed_graph" rel="noopener noreferrer"&gt;directed&lt;/a&gt; or &lt;a href="https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)#Undirected_graph" rel="noopener noreferrer"&gt;undirected&lt;/a&gt; (sometimes referred to as bidirectional)&lt;/li&gt;
&lt;li&gt;Ex. If we started at &lt;code&gt;A&lt;/code&gt;, we could go to &lt;code&gt;B&lt;/code&gt; or &lt;code&gt;C&lt;/code&gt; but we could not do the reverse&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Adjacency Lists
&lt;/h2&gt;

&lt;p&gt;An &lt;a href="https://en.wikipedia.org/wiki/Adjacency_list" rel="noopener noreferrer"&gt;adjacency list&lt;/a&gt; is a collection of neighbouring vertices relative to a given vertex. &lt;/p&gt;

&lt;p&gt;For the graph in Figure 1, We would say that vertex &lt;code&gt;B&lt;/code&gt; and vertex &lt;code&gt;C&lt;/code&gt; are adjacent to vertex &lt;code&gt;A&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The rest of vertices and their adjacent vertices are outlined below:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Vertex&lt;/th&gt;
&lt;th&gt;Adjacency List&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;A&lt;/td&gt;
&lt;td&gt;[ B, C ]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;B&lt;/td&gt;
&lt;td&gt;[ D, E ]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;C&lt;/td&gt;
&lt;td&gt;[ F ]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;D&lt;/td&gt;
&lt;td&gt;[ ]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;E&lt;/td&gt;
&lt;td&gt;[ ]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;F&lt;/td&gt;
&lt;td&gt;[ ]&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Depth First Search
&lt;/h2&gt;

&lt;p&gt;DFS is a traversal algorithm that: &lt;strong&gt;starts at the root (top most vertex) and exhaust all the branches of one neighbour before repeating for the next neighbour&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Given the graph from Figure 1, we would do these steps following the algorithm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Visit A
2. A has two neighbours: B and C
3. Visit B
4. B has two neighbours: D and E
5. Visit D
6. D has no neighbours so we've exhausted this branch
7. B has one more neighbour: E
8. Visit E
9. E has no neighbours so we've exhausted this branch
10. A has one more neighbour: C 
11. Visit C next
12. C has one neighbour: F
13. Visit F
14. F has no neighbours so we've exhausted this branch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can be better visualized as:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fq7gskxisynbs3w7m5v5e.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fq7gskxisynbs3w7m5v5e.gif" width="414" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's look at how we could implement DFS in Swift. First, we need to model a single vertex. One way could look like:&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="c1"&gt;// 1&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Vertex&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// 2&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;adjacencyList&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Vertex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

    &lt;span class="c1"&gt;// 3&lt;/span&gt;
    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&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;



&lt;ol&gt;
&lt;li&gt;A generic class definition for the vertex

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;T&lt;/code&gt; denotes that this is generic and can be of any type (i.e. &lt;code&gt;Int&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, etc)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The properties (or members) for the class

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;value&lt;/code&gt;: the generic data belonging to this vertex&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;visited&lt;/code&gt;: the flag with an initial value of &lt;code&gt;false&lt;/code&gt; to indicate if we've seen this node before&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;adjacencyList&lt;/code&gt;: the array that represents neighbouring vertices

&lt;ul&gt;
&lt;li&gt;This is one of the many ways to represent an adjacency list&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The initialization (or constructor) function

&lt;ul&gt;
&lt;li&gt;An example usage could look like: &lt;code&gt;Vertex&amp;lt;Int&amp;gt;(value: 2)&lt;/code&gt; or &lt;code&gt;Vertex&amp;lt;String&amp;gt;(value: "Alex")&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Using this model, there are three things to highlight with the following implementation of DFS:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It's &lt;a href="https://en.wikipedia.org/wiki/Recursion_(computer_science)" rel="noopener noreferrer"&gt;recursive&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;We're not searching for anything here instead we're printing out a value each time a vertex is visited&lt;/li&gt;
&lt;li&gt;It is possible for a vertex to point back to its parent vertex directly or indirectly

&lt;ul&gt;
&lt;li&gt;This is commonly seen in undirected graphs but possible in directed too&lt;/li&gt;
&lt;li&gt;It requires we keep track of vertices we've visited otherwise we could potentially traverse infinitely
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;depthFirstSearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="nv"&gt;vertex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Vertex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// 2&lt;/span&gt;
    &lt;span class="n"&gt;vertex&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;visited&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="c1"&gt;// 3&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vertex&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;// 4&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;adjacentVertex&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;vertex&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;adjacencyList&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// 5&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;adjacentVertex&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;visited&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// 6&lt;/span&gt;
            &lt;span class="nf"&gt;depthFirstSearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;adjacentVertex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&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;



&lt;ol&gt;
&lt;li&gt;Defines a function that takes in one parameter &lt;code&gt;vertex&lt;/code&gt; of optional type &lt;code&gt;Vertex&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;?&lt;/code&gt; denotes this is an &lt;a href="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/doc/uid/TP40014097-CH31-ID452" rel="noopener noreferrer"&gt;optional type&lt;/a&gt; and means that this value could be nil &lt;/li&gt;
&lt;li&gt;The &lt;code&gt;from&lt;/code&gt; is a named parameter and helps the function name read like a sentence: &lt;code&gt;depthFirstSearch(from: someRootVertex)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Mark the &lt;code&gt;vertex&lt;/code&gt; as visited so we don't accidentally visit it again&lt;/li&gt;
&lt;li&gt;Print the &lt;code&gt;value&lt;/code&gt; property of the &lt;code&gt;vertex&lt;/code&gt; followed by a new line&lt;/li&gt;
&lt;li&gt;We iterate through all the root's adjacent vertices denoting the iteration variable as &lt;code&gt;adjacentVertex&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;We check if we haven't visited the &lt;code&gt;adjacentVertex&lt;/code&gt; before doing anything&lt;/li&gt;
&lt;li&gt;Continue the traversal by calling &lt;code&gt;depthFirstSearch(from:)&lt;/code&gt; again and passing &lt;code&gt;adjacentVertex&lt;/code&gt; as the new vertex&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Steps &lt;code&gt;1.&lt;/code&gt; to &lt;code&gt;6.&lt;/code&gt; will repeat until we've exhausted all the vertices in the graph.&lt;br&gt;
The output would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A
B
D
E
C
F
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another way we could implement DFS is using a stack but that's out of scope for this post.&lt;/p&gt;

&lt;h2&gt;
  
  
  UIView &lt;code&gt;description&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Before we can look at &lt;code&gt;recursiveDescription&lt;/code&gt; we need to first look at its counterpart &lt;code&gt;description&lt;/code&gt;:&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="c1"&gt;// Swift generated version of NSObject.h&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;description&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="k"&gt;get&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://developer.apple.com/documentation/objectivec/nsobject/1418799-description" rel="noopener noreferrer"&gt;description&lt;/a&gt; is a property that exists on all Objective-C classes that inherit from &lt;code&gt;NSObject&lt;/code&gt;. This property returns a string representation of the object's contents. This is similar to &lt;code&gt;__str__&lt;/code&gt;/&lt;code&gt;__repr__&lt;/code&gt; in Python. &lt;/p&gt;

&lt;p&gt;If you ever had to debug something in iOS then you've likely called &lt;code&gt;description&lt;/code&gt; directly or indirectly.&lt;br&gt;
The way the description would typically be called is as follows:&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="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;view&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CGRect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;UIView: 0x7fecf2106100; frame = (0 10; 100 500); layer = &amp;lt;CALayer: 0x600000028500&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can override the &lt;code&gt;description&lt;/code&gt; function to provide a custom description message:&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;class&lt;/span&gt; &lt;span class="kt"&gt;Tutorial&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;NSObject&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;title&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="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;/// This overrides the superclass description&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;description&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="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;Tutorial: &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;&amp;gt;"&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;



&lt;p&gt;The custom &lt;code&gt;description&lt;/code&gt; would be called as:&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="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;tutorial&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Tutorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"This is a tutorial about Cats"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tutorial&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Tutorial: "This is a tutorial about Cats"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a pure Swift class (one that does not inherit from NSObject) or struct, you can achieve this via protocols such &lt;a href="https://developer.apple.com/documentation/swift/customstringconvertible" rel="noopener noreferrer"&gt;CustomStringConvertible&lt;/a&gt; and &lt;a href="https://developer.apple.com/documentation/swift/customdebugstringconvertible" rel="noopener noreferrer"&gt;CustomDebugStringConvertible&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;These protocols could be used as follows:&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;struct&lt;/span&gt; &lt;span class="kt"&gt;Tutorial&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CustomStringConvertible&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;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;

    &lt;span class="c1"&gt;// MARK: CustomStringConvertible&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;description&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="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;Tutorial: &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;&amp;gt;"&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;



&lt;h2&gt;
  
  
  UIView &lt;code&gt;recursiveDescription&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;For a single view, &lt;code&gt;description&lt;/code&gt; is often enough but what if you wanted to get information about its view hierarchy? That is where &lt;code&gt;recursiveDescription&lt;/code&gt; comes in. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;recursiveDescription&lt;/code&gt; is a private function on UIView that prints the &lt;code&gt;description&lt;/code&gt; of the view and all its subviews (or children views). &lt;br&gt;
However, using it is one of those things that's easier in Objective-C but still possible in Swift. We just have to do some &lt;a href="https://stackoverflow.com/a/27694502/1631577" rel="noopener noreferrer"&gt;extra steps&lt;/a&gt; to get it to work.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Please note that since this a private API it should NOT be shipped in production code. Your app is likely get rejected from the App Store. For debugging purposes though it should be fine.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We're going to set up a simple view hierarchy in the below code snippet. &lt;br&gt;
This setup is heavily simplified and likely not how you would actually setup a UI since:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We're not taking view layout into account; and &lt;/li&gt;
&lt;li&gt;Some of these views such as &lt;code&gt;tableView&lt;/code&gt; shouldn't have subviews added to them.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;scrollView&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIScrollView&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;label1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UILabel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;label2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UILabel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;scrollView&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addSubview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;label1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;scrollView&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addSubview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;label2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;tableView&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UITableView&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;imageView&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIImageView&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;tableView&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addSubview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;imageView&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// 3&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;view&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIView&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addSubview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scrollView&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addSubview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tableView&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// 4&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;perform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"recursiveDescription"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Creates a &lt;code&gt;UIScrollView&lt;/code&gt; and adds two labels: &lt;code&gt;label1&lt;/code&gt; and &lt;code&gt;label2&lt;/code&gt; as subviews&lt;/li&gt;
&lt;li&gt;Creates a &lt;code&gt;UITableView&lt;/code&gt; and adds a single &lt;code&gt;UImageView&lt;/code&gt; as a subview&lt;/li&gt;
&lt;li&gt;Creates a &lt;code&gt;UIView&lt;/code&gt; and adds the &lt;code&gt;scrollView&lt;/code&gt; and &lt;code&gt;tableView&lt;/code&gt; from above as subviews&lt;/li&gt;
&lt;li&gt;Calls &lt;code&gt;recursiveDescription&lt;/code&gt; on the &lt;code&gt;view&lt;/code&gt; via the &lt;code&gt;perform()&lt;/code&gt; function

&lt;ul&gt;
&lt;li&gt;Since this is a private function, we can't just call &lt;code&gt;view.recursiveDescription()&lt;/code&gt; as it won't compile&lt;/li&gt;
&lt;li&gt;Instead we call this function via &lt;code&gt;perform()&lt;/code&gt;. This lets us call an arbitrary function on an object by its name&lt;/li&gt;
&lt;li&gt;This approach to function calling is &lt;strong&gt;not&lt;/strong&gt; recommended though as it'll crash if the object does not implement it&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The output should look something like this minus the comments &lt;code&gt;//&lt;/code&gt; and &lt;code&gt;[...]&lt;/code&gt; which is used to truncate the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;UIView: 0x7fcf29812970; [...]&amp;gt; // view
   | &amp;lt;UIScrollView: 0x7fcf2901b800; [...]&amp;gt; // scrollView
   |    | &amp;lt;UILabel: 0x7fcf29808710; [...]&amp;gt; // label1
   |    | &amp;lt;UILabel: 0x7fcf26e021d0; [...]&amp;gt; // label2
   | &amp;lt;UITableView: 0x7fcf2903f200; [...]&amp;gt; // tableView
   |    | &amp;lt;UIImageView: 0x7fcf29810f80; [...]&amp;gt; // imageView
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above output shows each view description along with the description of its subviews indented to represent depth.&lt;/p&gt;

&lt;p&gt;The indent is denoted with a pipe (&lt;code&gt;|&lt;/code&gt;) and spaces. You can see that this matches our initial code: &lt;code&gt;view&lt;/code&gt; is the parent of &lt;code&gt;scrollView&lt;/code&gt; and &lt;code&gt;tableView&lt;/code&gt; which are parents of their own subviews.&lt;/p&gt;

&lt;p&gt;We can conclude from the above output that the following is happening:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Visit view
2. view has two subviews: scrollView and tableView
3. Visit scrollView
4. scrollView has two subviews: label1 and label2
5. Visit label1
6. label1 has no subviews so we've exhausted this view
7. scrollView has one more subview: label2
8. Visit label2
9. label2 has no subviews so we've exhausted this view
10. view has one more subview: tableView
11. Visit tableView
12. tableView has one subview: imageView
13. Visit imageView
14. imageView has no subviews so we've exhausted this view
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the above look familiar to you then you're noticing something very important. These steps follow the same algorithm as the steps in the "Depth First Search" traversal.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reverse engineering &lt;code&gt;recursiveDescription&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;It looks like &lt;code&gt;recursiveDescription&lt;/code&gt; is using DFS to print out its hierarchy but how can we confirm this without looking at the source code?&lt;/p&gt;

&lt;p&gt;Our only option is to attempt to reverse engineer the function. &lt;br&gt;
Since we don't know how it works we have to treat the function as a black box. We can observe what the output is for varying inputs.&lt;/p&gt;

&lt;p&gt;Additionally, it'll help to simplify what we want to print out in our version of &lt;code&gt;recursiveDescription&lt;/code&gt;. &lt;br&gt;
We'll change the previous output to ignore the spaces around the pipes (&lt;code&gt;|&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;UIView: 0x7fcf29812970; [...]&amp;gt;
|&amp;lt;UIScrollView: 0x7fcf2901b800; [...]&amp;gt;
||&amp;lt;UILabel: 0x7fcf29808710; [...]&amp;gt;
||&amp;lt;UILabel: 0x7fcf26e021d0; [...]&amp;gt;
|&amp;lt;UITableView: 0x7fcf2903f200; [...]&amp;gt;
||&amp;lt;UIImageView: 0x7fcf29810f80; [...]&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the output represents a hierarchy, you might notice it can be represented similar to the graph in Figure 1 as:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Figure 2. UIView hierarchy graph&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fx7tgu3uhxvgd3mk27hg9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fx7tgu3uhxvgd3mk27hg9.png" width="800" height="594"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Trivial View Hierarchy
&lt;/h2&gt;

&lt;p&gt;Let's try to solve the simple case: a hierarchy consisting of a single UIView.&lt;br&gt;
If we only have one view then we just need to print out its description.&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="c1"&gt;// 1&lt;/span&gt;
&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;UIView&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// 2&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;recursiveDescription&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// 3&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;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;



&lt;ol&gt;
&lt;li&gt;Create an extension so we can add our &lt;code&gt;recursiveDescription&lt;/code&gt; function to UIView

&lt;ul&gt;
&lt;li&gt;An &lt;code&gt;extension&lt;/code&gt; lets you add functions to an existing class&lt;/li&gt;
&lt;li&gt;This is especially useful when you don't have access to the class internals&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Defines a function called &lt;code&gt;recursiveDescription&lt;/code&gt; that takes no parameters and returns a &lt;code&gt;String&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Return the &lt;code&gt;description&lt;/code&gt; of the view&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's test out what happens when we print the &lt;code&gt;recursiveDescription&lt;/code&gt; for a single view using our implementation:&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="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;view&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIView&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;recursiveDescription&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output should look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;UIView: 0x7fcf29812970; [...]&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, that isn't very exciting, is it? If we have a more complex hierarchy then it'll only ever print the parent view.&lt;/p&gt;

&lt;p&gt;We have be able to print the parent view's &lt;code&gt;description&lt;/code&gt; along with all of the subview &lt;code&gt;description&lt;/code&gt;s.&lt;/p&gt;

&lt;h2&gt;
  
  
  Non-Trivial View Hierarchy
&lt;/h2&gt;

&lt;p&gt;We now need a way of iterating through the subviews of a view and we can do that via the UIView property &lt;code&gt;subviews&lt;/code&gt;. This property returns an array of the immediate subviews for a given view.&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="c1"&gt;// Swift generated version of UIView.h&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;subviews&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;UIView&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should remind you of an adjacency list and we can also model the view hierarchy similarly:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Table 2. View and Subviews from Snippet 1&lt;/em&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;UIView&lt;/th&gt;
&lt;th&gt;Subviews&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;view&lt;/td&gt;
&lt;td&gt;[ scrollView, tableView ]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;scrollView&lt;/td&gt;
&lt;td&gt;[ label1, label2 ]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;tableView&lt;/td&gt;
&lt;td&gt;[ imageview ]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;label1&lt;/td&gt;
&lt;td&gt;[ ]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;label2&lt;/td&gt;
&lt;td&gt;[ ]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;imageView&lt;/td&gt;
&lt;td&gt;[ ]&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;With the ability to get subviews we can update our original &lt;code&gt;recursiveDescription&lt;/code&gt; implementation to match a traditional DFS implementation as follows:&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="c1"&gt;// The extension UIView code is present but omitted for simplicity&lt;/span&gt;
&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;recursiveDescription&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// 1&lt;/span&gt;
    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;subviews&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isEmpty&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// 2&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;
    &lt;span class="c1"&gt;// 3&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;subviews&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// 4&lt;/span&gt;
        &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;recursiveDescription&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// 5&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Assert that this view has subviews by checking if its subviews array is empty

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;guard&lt;/code&gt; is a Swift feature that acts like an assertion. If the assertion fails then it enters the &lt;code&gt;else&lt;/code&gt; block&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Initializes a local variable &lt;code&gt;text&lt;/code&gt; with the description of the current view we're at&lt;/li&gt;
&lt;li&gt;Iterates through each of the views in &lt;code&gt;subviews&lt;/code&gt; denoting each as &lt;code&gt;view&lt;/code&gt; (singular)&lt;/li&gt;
&lt;li&gt;Appends the results of calling &lt;code&gt;recursiveDescription()&lt;/code&gt; on each &lt;code&gt;view&lt;/code&gt; to &lt;code&gt;text&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;Since a view cannot have its parent as a subview, we don't need to check if we've visited it already&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Returns the final version &lt;code&gt;text&lt;/code&gt; to be used by the caller&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This will yield us a result that looks very similar to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;UIView: 0x7fa2db70ebe0; [...]&amp;gt;&amp;lt;UIScrollView: 0x7fa2de80d000; [...]&amp;gt;&amp;lt;UILabel: 0x7fa2db400b60; [...]&amp;gt;&amp;lt;UILabel: 0x7fa2db706520; [...]&amp;gt;&amp;lt;UITableView: 0x7fa2dd047c00; [...]&amp;gt;&amp;lt;UIImageView: 0x7fa2db70d1e0; frame = (0 0; 0 0); [...]&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happened? It looks like we forgot to add new lines after each print.&lt;/p&gt;

&lt;p&gt;Let's replace our line that appends each &lt;code&gt;subview.recursiveDescription()&lt;/code&gt; to have a prefixed new-line character (&lt;code&gt;\n&lt;/code&gt;).&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="c1"&gt;// The extension UIView code is present but omitted for simplicity&lt;/span&gt;
&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;recursiveDescription&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;subviews&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// 1&lt;/span&gt;
        &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;recursiveDescription&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;This appends a newline character (&lt;code&gt;\n&lt;/code&gt;) to the text prior to appending the recursiveDescription of the &lt;code&gt;view&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The output now looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;UIView: 0x7fa2db70ebe0; [...]&amp;gt;
&amp;lt;UIScrollView: 0x7fa2de80d000; [...]&amp;gt;
&amp;lt;UILabel: 0x7fa2db400b60; [...]&amp;gt;
&amp;lt;UILabel: 0x7fa2db706520; [...]&amp;gt;
&amp;lt;UITableView: 0x7fa2dd047c00; [...]&amp;gt;
&amp;lt;UIImageView: 0x7fa2db70d1e0; [...]&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is better but there is no indentation representing hierarchy depth.&lt;/p&gt;

&lt;h3&gt;
  
  
  Expanding &lt;code&gt;recursiveDescription&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;How do we indicate what level we're on and how do we get those pipes (&lt;code&gt;|&lt;/code&gt;) to display?&lt;br&gt;
Since we're recursively calling our function we can pass down data through a function parameter.&lt;/p&gt;

&lt;p&gt;We could extend our function to include a &lt;code&gt;"prefix"&lt;/code&gt; parameter and at each level we'll pass down what to prepend before each output.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In level 1, &lt;code&gt;prefix&lt;/code&gt; is &lt;code&gt;""&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;In level 2, &lt;code&gt;prefix&lt;/code&gt; is &lt;code&gt;"|"&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;In level 3, &lt;code&gt;prefix&lt;/code&gt; is &lt;code&gt;"||"&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;In level 4, &lt;code&gt;prefix&lt;/code&gt; is &lt;code&gt;"|||"&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;In level n, &lt;code&gt;prefix&lt;/code&gt; is &lt;code&gt;"||...||"&lt;/code&gt; (&lt;code&gt;"|"&lt;/code&gt; repeated n-1 times)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, since the original implementation of &lt;code&gt;recursiveDescription&lt;/code&gt; takes no parameters, we'll need to create a helper function to handle the &lt;code&gt;prefix&lt;/code&gt; passing and recursive nature of this function. &lt;/p&gt;

&lt;p&gt;We'll call it &lt;code&gt;recursiveDescriptionHelper&lt;/code&gt; and it'll take a single &lt;code&gt;prefix&lt;/code&gt; parameter of type &lt;code&gt;String&lt;/code&gt;:&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="c1"&gt;// The extension UIView code is present but omitted for simplicity&lt;/span&gt;
&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;recursiveDescription&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;recursiveDescriptionHelper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;recursiveDescriptionHelper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="nv"&gt;prefix&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="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;subviews&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isEmpty&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;description&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;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;
    &lt;span class="c1"&gt;// 1&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;nextPrefix&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;prefix&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"|"&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;subviews&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;// 2&lt;/span&gt;
        &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nextPrefix&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;// 3&lt;/span&gt;
        &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;recursiveDescriptionHelper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;nextPrefix&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Append a pipe (&lt;code&gt;|&lt;/code&gt;) to the &lt;code&gt;prefix&lt;/code&gt; parameter and store it in the local variable &lt;code&gt;nextPrefix&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;We'll append the &lt;code&gt;nextPrefix&lt;/code&gt; to the text prior to appending the view's &lt;code&gt;recursiveDescription&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Call &lt;code&gt;recursiveDescriptionHelper&lt;/code&gt; and pass in the &lt;code&gt;nextPrefix&lt;/code&gt; to use for the next recursive call

&lt;ul&gt;
&lt;li&gt;Recall the recursive calls will stop once the view has no more subviews&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Afterwards, we should expect to see an output like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;UIView: 0x7fa2db70ebe0; [...]&amp;gt;
|&amp;lt;UIScrollView: 0x7fa2de80d000; [...]&amp;gt;
||&amp;lt;UILabel: 0x7fa2db400b60; [...]&amp;gt;
||&amp;lt;UILabel: 0x7fa2db706520; [...]&amp;gt;
|&amp;lt;UITableView: 0x7fa2dd047c00; [...]&amp;gt;
||&amp;lt;UIImageView: 0x7fa2db70d1e0; [...]&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This matches the expected output of our version of &lt;code&gt;recursiveDescription&lt;/code&gt; but it doesn't technically match what the real &lt;code&gt;recursiveDescription&lt;/code&gt; does. &lt;/p&gt;

&lt;p&gt;Hopefully, you see how DFS could have been used for the real implementation and how we can expand on it from here.&lt;br&gt;
Hopefully, you see how DFS could have been used for the real implementation and how we can expand on it from here. I've put the final version together for you in this &lt;a href="https://github.com/ajfigueroa/blog-code/tree/master/posts/1-DFSubviews.playground" rel="noopener noreferrer"&gt;playground&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this post, we looked at DFS and how a potential application exists in the function &lt;code&gt;recursiveDescription&lt;/code&gt;.&lt;br&gt;
We also briefly touched on how to reverse engineer a function by treating it as a black box. &lt;/p&gt;

&lt;p&gt;I hoped this helped inspire you to look out for some of these famous algorithms in your day to day life.&lt;br&gt;
It really helps make the concept stick if you can see a practical application for it. &lt;/p&gt;

&lt;p&gt;Let me know if you have any suggestions or questions, I'd really appreciate the feedback.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.raywenderlich.com/152046/swift-algorithm-club-graphs-adjacency-list" rel="noopener noreferrer"&gt;Adjacency Lists&lt;/a&gt; and&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.raywenderlich.com/157949/swift-algorithm-club-depth-first-search" rel="noopener noreferrer"&gt;Depth First Search&lt;/a&gt; in great detail.&lt;/li&gt;
&lt;li&gt;A &lt;a href="https://developer.apple.com/library/content/technotes/tn2239/_index.html#//apple_ref/doc/uid/DTS40010638-CH1-SUBSECTION34" rel="noopener noreferrer"&gt;blurb&lt;/a&gt; in Apple documentation about &lt;code&gt;recursiveDescription&lt;/code&gt; usage.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Originally posted on my &lt;a href="https://alexanderfigueroa.com" rel="noopener noreferrer"&gt;website&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>dfs</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
