<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Eman Harout | Programming Blog</title><description>A blog that explores topics which spark intrigue.</description><link>https://emanharout.com/</link><language>en-us</language><item><title>Swift Property Wrappers Gist</title><link>https://emanharout.com/blog/swift-property-wrappers-gist/</link><guid isPermaLink="true">https://emanharout.com/blog/swift-property-wrappers-gist/</guid><description>A quick tour of Swift property wrappers and how they encapsulate logic for getting and setting properties.</description><pubDate>Tue, 01 Apr 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Swift property wrappers provide a way to encapsulate logic for getting an setting properties. Here’s a quick tour of their utility.&lt;/p&gt;
&lt;section&gt;&lt;h2 id=&quot;creating-a-property-wrapper&quot;&gt;Creating a Property Wrapper&lt;/h2&gt;&lt;p&gt;Here is an example of how to create and use a property wrapper:&lt;/p&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;// Create&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot; class=&quot;highlighted-word&quot;&gt;@propertyWrapper&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#ACAFFF&quot;&gt; UpperCase&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; storedValue: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot; class=&quot;highlighted-word&quot;&gt;wrappedValue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;        set&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; { storedValue &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; newValue }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;        get&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; { storedValue.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#EEF0F9&quot;&gt;uppercased&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;() }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot; class=&quot;highlighted-word&quot;&gt;init&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#00DAEF;--shiki-dark-font-style:italic&quot;&gt;wrappedValue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#4BF3C8&quot;&gt;wrappedValue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; wrappedValue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;// Use&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#ACAFFF&quot;&gt; Address&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot; class=&quot;highlighted-word&quot;&gt;@UpperCase&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; state: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; address &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt; Address&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#FFD493&quot;&gt;&amp;quot;California&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;address.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#4BF3C8&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt; // CALIFORNIA&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;ol&gt;
&lt;li&gt;Annotate the struct with &lt;code&gt;@propertyWrapper&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Define a &lt;code&gt;wrappedValue&lt;/code&gt; variable that applies formatting and/or logic.&lt;/li&gt;
&lt;li&gt;Add an initializer&lt;/li&gt;
&lt;li&gt;Annotate the property with @UpperCase to apply the property wrapper.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Optional: can also use &lt;code&gt;didSet&lt;/code&gt; instead of &lt;code&gt;get&lt;/code&gt; and &lt;code&gt;set&lt;/code&gt;, but this requires repeating formatting/logic in the initializer, since &lt;code&gt;didSet&lt;/code&gt; does not execute on the initial assignment.&lt;/p&gt;&lt;section&gt;&lt;h3 id=&quot;initializers-and-default-values&quot;&gt;Initializers and Default Values&lt;/h3&gt;&lt;p&gt;There are a few ways to initialize types using property wrappers. For demonstration purposes, I have adjusted the property wrapper like so:&lt;/p&gt;&lt;ol&gt;
&lt;li&gt;Added a new &lt;code&gt;prefix&lt;/code&gt; property to limit the number of characters to uppercase&lt;/li&gt;
&lt;li&gt;Declare three initializers&lt;/li&gt;
&lt;/ol&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;@propertyWrapper&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#ACAFFF&quot;&gt; UpperCase&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; storedValue: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; prefixNumber: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; wrappedValue: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;        set&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; { storedValue &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; newValue }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;        get&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;            let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; caps &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; storedValue.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#EEF0F9&quot;&gt;uppercased&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;            let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; prefixed &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; caps.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#EEF0F9&quot;&gt;prefix&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(prefixNumber)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt; String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(prefixed)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;    // MARK: - Initializers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot; class=&quot;highlighted-word&quot;&gt;init&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#4BF3C8&quot;&gt;prefixNumber&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#FFD493&quot;&gt; 3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#4BF3C8&quot;&gt;wrappedValue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#FFD493&quot;&gt; &amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot; class=&quot;highlighted-word&quot;&gt;init&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#00DAEF;--shiki-dark-font-style:italic&quot;&gt;wrappedValue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#4BF3C8&quot;&gt;prefixNumber&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#FFD493&quot;&gt; 3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#4BF3C8&quot;&gt;wrappedValue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; wrappedValue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot; class=&quot;highlighted-word&quot;&gt;init&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#00DAEF;--shiki-dark-font-style:italic&quot;&gt;wrappedValue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#00DAEF;--shiki-dark-font-style:italic&quot;&gt;prefix&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;Int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#4BF3C8&quot;&gt;prefixNumber&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; prefix&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#4BF3C8&quot;&gt;wrappedValue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; wrappedValue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;section&gt;&lt;h4 id=&quot;using-init&quot;&gt;Using init()&lt;/h4&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#ACAFFF&quot;&gt; Address&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    @UpperCase&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; state: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; address &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt; Address&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot; class=&quot;highlighted-word&quot;&gt;// Calls init()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;address.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#4BF3C8&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt; // &amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/section&gt;&lt;section&gt;&lt;h4 id=&quot;using-initwrappedvalue&quot;&gt;Using init(wrappedValue:)&lt;/h4&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; addressTwo &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt; Address&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;UpperCase&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;wrappedValue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#FFD493&quot;&gt;&amp;quot;Cali&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot; class=&quot;highlighted-word&quot;&gt;// Calls init(wrappedValue:)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;addressTwo.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#4BF3C8&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt; // CAL&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Note we must pass in &lt;code&gt;UpperCase&lt;/code&gt; instance to set &lt;code&gt;state&lt;/code&gt;’s value.&lt;/p&gt;&lt;p&gt;Alternatively, we can directly inject a string by modifying &lt;code&gt;Address&lt;/code&gt;:&lt;/p&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#ACAFFF&quot;&gt; AddressV2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    @UpperCase&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; state: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#FFD493&quot;&gt; &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot; class=&quot;highlighted-word&quot;&gt;// Calls init(wrappedValue:)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; addressV2_default &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt; AddressV2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot; class=&quot;highlighted-word&quot;&gt;// Calls init(wrappedValue:)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;addressV2_default.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#4BF3C8&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt; // NEW&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; addressV2_california &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt; AddressV2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#FFD493&quot;&gt;&amp;quot;California&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot; class=&quot;highlighted-word&quot;&gt;// Calls init(wrappedValue:)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;addressV2_california.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#4BF3C8&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt; // CAL&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;These examples use &lt;code&gt;init(wrappedValue:)&lt;/code&gt;.&lt;/p&gt;&lt;/section&gt;&lt;section&gt;&lt;h4 id=&quot;using-initwrappedvalue-prefix&quot;&gt;Using init(wrappedValue: prefix:)&lt;/h4&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#ACAFFF&quot;&gt; AddressV3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    @UpperCase&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(wrappedValue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#FFD493&quot;&gt; &amp;quot;CityDefault&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;prefix:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#FFD493&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; city: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot; class=&quot;highlighted-word&quot;&gt;// Calls init(wrappedValue: prefix:)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    @UpperCase&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;prefix:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#FFD493&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; state: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#FFD493&quot;&gt; &amp;quot;StateDefault&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot; class=&quot;highlighted-word&quot;&gt;// Calls init(wrappedValue: prefix:)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; cityStateAddress &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt; AddressV3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot; class=&quot;highlighted-word&quot;&gt;// Calls init(wrappedValue: prefix:)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;cityStateAddress.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#4BF3C8&quot;&gt;city&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt; // CITYD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;cityStateAddress.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#4BF3C8&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt; // STATE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Both properties use &lt;code&gt;init(wrappedValue: prefix:)&lt;/code&gt; to initialize the property wrapper.&lt;/p&gt;&lt;/section&gt;&lt;/section&gt;&lt;/section&gt;
&lt;section&gt;&lt;h2 id=&quot;projectedvalue&quot;&gt;ProjectedValue&lt;/h2&gt;&lt;p&gt;A projected value provides added utility to property wrappers.&lt;/p&gt;&lt;p&gt;In this example, we create a &lt;code&gt;projectedValue&lt;/code&gt; that returns &lt;code&gt;true&lt;/code&gt; if the original string exceeds the defined prefix length:&lt;/p&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston has-highlighted&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;@propertyWrapper&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#ACAFFF&quot;&gt; UpperCase&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; storedValue: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; prefixNumber: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line highlighted&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; projectedValue: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;Bool&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; { storedValue.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#EEF0F9&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; prefixNumber } &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; wrappedValue: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;        set&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; { storedValue &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; newValue }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;        get&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;            let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; caps &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; storedValue.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#EEF0F9&quot;&gt;uppercased&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;            let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; prefixed &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; caps.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#EEF0F9&quot;&gt;prefix&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(prefixNumber)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt; String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(prefixed)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    init&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#00DAEF;--shiki-dark-font-style:italic&quot;&gt;wrappedValue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#00DAEF;--shiki-dark-font-style:italic&quot;&gt;prefix&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;Int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#4BF3C8&quot;&gt;prefixNumber&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; prefix&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#4BF3C8&quot;&gt;wrappedValue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; wrappedValue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;To reference &lt;code&gt;projectedValue&lt;/code&gt;, we use the &lt;code&gt;$&lt;/code&gt; symbol before the property name:&lt;/p&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#ACAFFF&quot;&gt; Address&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    @UpperCase&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; state: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; address &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt; Address&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;UpperCase&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;wrappedValue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#FFD493&quot;&gt;&amp;quot;Cali&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;prefix&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#FFD493&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;address.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot; class=&quot;highlighted-word&quot;&gt;$state&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;// true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;address.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#4BF3C8&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#FFD493&quot;&gt; &amp;quot;C&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;address.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot; class=&quot;highlighted-word&quot;&gt;$state&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;// false&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/section&gt;
&lt;section&gt;&lt;h2 id=&quot;accessing-the-property-wrapper&quot;&gt;Accessing the Property Wrapper&lt;/h2&gt;&lt;p&gt;To access the backing storage/property wrapper itself, use underscore:&lt;/p&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#EEF0F9&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(state) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;// prints the string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#EEF0F9&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(_state) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;// prints the property value&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/section&gt;
&lt;section&gt;&lt;h2 id=&quot;function-parameters&quot;&gt;Function Parameters&lt;/h2&gt;&lt;p&gt;Swift allows for the use of property wrappers in function parameters.&lt;/p&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#00DAEF&quot;&gt; toUpperCase&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;highlighted-word&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#00DAEF&quot;&gt;UpperCase&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F9;--shiki-dark-font-style:italic&quot;&gt; message&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#EEF0F9&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(message)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;toUpperCase&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#FFD493&quot;&gt;&amp;quot;new york&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;// prints &amp;quot;NEW&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can configure the property wrapper with default arguments too:&lt;/p&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#00DAEF&quot;&gt; toUpperCase&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;highlighted-word&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;@UpperCase(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#00DAEF;--shiki-dark-font-style:italic&quot;&gt;prefix&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: 9)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; message: String) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#EEF0F9&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(message)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;toUpperCase&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#FFD493&quot;&gt;&amp;quot;california&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;// prints &amp;quot;CALIFORNI&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/section&gt;</content:encoded><category>programming</category><category>swift</category><author>Eman Harout</author></item><item><title>Introduction to Swift Metatypes</title><link>https://emanharout.com/blog/introduction-to-swift-metatypes/</link><guid isPermaLink="true">https://emanharout.com/blog/introduction-to-swift-metatypes/</guid><description>Metatypes can be difficult to grasp. Learn what .Type and .self mean and how to use them.</description><pubDate>Wed, 14 Apr 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Metatypes can be difficult to grasp, both in concept and application. My hope is that after reading this, you’ll have mastered the basics of metatypes!&lt;/p&gt;
&lt;section&gt;&lt;h2 id=&quot;instances--types&quot;&gt;Instances &amp;amp; Types&lt;/h2&gt;&lt;p&gt;&lt;img src=&quot;https://emanharout.com/_astro/blog_intro-to-swift-metatypes_swift-metatypes-chart.CGOjCve8_Z1tAWu4.webp&quot; alt=&quot;Metatypes Chart&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; fetchpriority=&quot;auto&quot; width=&quot;2048&quot; height=&quot;1152&quot;&gt;&lt;/p&gt;&lt;p&gt;To understand metatypes, it helps to start with the familiar. For example, just as &lt;code&gt;String&lt;/code&gt; is the type of &lt;code&gt;&amp;quot;Hello World&amp;quot;&lt;/code&gt;, &lt;code&gt;String.Type&lt;/code&gt; is the type of &lt;code&gt;String.self&lt;/code&gt;. While &lt;code&gt;String&lt;/code&gt; is the type of an instance, &lt;code&gt;String.Type&lt;/code&gt; is the type of a type—hence the name “metatype”.&lt;/p&gt;&lt;p&gt;Here is an illustration in code:&lt;/p&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; myString: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#FFD493&quot;&gt; &amp;quot;Hello World!&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; myStringType: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;Type&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt; String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;self&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And since a metatype is a type, we can specify it as such in function signatures:&lt;/p&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#00DAEF&quot;&gt; doSomething&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#00DAEF&quot;&gt;with&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F9;--shiki-dark-font-style:italic&quot;&gt; type&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;.T&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;ype) { &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; } &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;// Specify a metatype parameter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;doSomething&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;with&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;// Pass in a metatype instance&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This works if you know what metatype instance you want to pass in at compile time. But what if you have an object and need to get its metatype at runtime? Never fear! Simply use the &lt;code&gt;type(of:)&lt;/code&gt; method, like so:&lt;/p&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; myString &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#FFD493&quot;&gt; &amp;quot;Hello, world!&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; myStringType &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#EEF0F9&quot;&gt; type&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#EEF0F9&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: myString) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;// converts an instance to its metatype value.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;doSomething&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;with&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: myStringType)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This seems pointless with &lt;code&gt;String&lt;/code&gt;, but if you have a class with many subclasses, you wouldn’t know which subclass-type you’re dealing with. Passing the object to &lt;code&gt;type(of:)&lt;/code&gt; allows you to get the metatype instance.&lt;/p&gt;&lt;p&gt;Now that we know how to create instances of a metatype, let’s briefly discuss what we’re able to do with them.&lt;/p&gt;&lt;/section&gt;
&lt;section&gt;&lt;h2 id=&quot;application&quot;&gt;Application&lt;/h2&gt;&lt;p&gt;Metatypes are used to access properties and methods that belong to the type. This includes anything marked as &lt;code&gt;static&lt;/code&gt;, &lt;code&gt;class&lt;/code&gt;, and &lt;code&gt;init&lt;/code&gt;.&lt;/p&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#ACAFFF&quot;&gt; SomeClass&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;  static&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; someKey &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#FFD493&quot;&gt; &amp;quot;0000&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;  init&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;() { &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;  static&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#00DAEF&quot;&gt; someMethod&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;() { &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; key &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; SomeClass.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#4BF3C8&quot;&gt;someKey&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt; // Shorthand for SomeClass.self.someKey&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; someClass &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt; SomeClass&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;// Shorthand for SomeClass.self.init()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;SomeClass.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;someMethod&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;// Shorthand for, you guessed it, SomeClass.self.someMethod()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;While we don’t see a metatype instance, we are actually using metatypes to perform all these actions! For instance, &lt;code&gt;SomeClass.someKey&lt;/code&gt; is the same as &lt;code&gt;SomeClass.self.someKey&lt;/code&gt;, but Swift hides all the clutter from us. I typically add properties and methods to types when there’s a piece of functionality that isn’t dependent on an instance.&lt;/p&gt;&lt;p&gt;Fun fact, this is exactly what is being done when you pass a metatype to a &lt;code&gt;JSONDecoder&lt;/code&gt;’s decode method:&lt;/p&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;// JSONDecoder&amp;#39;s method&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#00DAEF&quot;&gt; decode&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;&amp;gt;(T.Type, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#00DAEF;--shiki-dark-font-style:italic&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: Data) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; T { &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; decoder &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt; JSONDecoder&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;decoder.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#EEF0F9&quot;&gt;decode&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(SomeClass.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: someData) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;// now JSON Decoder has access to SomeClass&amp;#39; init method.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/section&gt;
&lt;section&gt;&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;&lt;p&gt;To summarize:&lt;/p&gt;&lt;ul&gt;
&lt;li&gt;A metatype is simply a type with &lt;code&gt;.Type&lt;/code&gt; appended to it, e.g. &lt;code&gt;String.Type&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;A metatype instance can be specified by appending &lt;code&gt;.self&lt;/code&gt; to a type, e.g. &lt;code&gt;String.self&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;To get a metatype instance at runtime, simply pass an instance to &lt;code&gt;type(of:)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Metatypes allow access to properties, methods, and initializers belonging to the type.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;I hope you found this explanation of metatypes useful! We have only scratched the surface, however. For a deeper dive with more examples, please see &lt;a href=&quot;https://swiftrocks.com/whats-type-and-self-swift-metatypes&quot; rel=&quot;nofollow noopener noreferrer&quot; target=&quot;_blank&quot;&gt;Bruno Rocha’s excellent post on Metatypes!&lt;/a&gt;&lt;/p&gt;&lt;/section&gt;</content:encoded><category>programming</category><category>swift</category><author>Eman Harout</author></item><item><title>Swift’s Guard Statement: Purpose and Usage</title><link>https://emanharout.com/blog/swifts-guard-statement/</link><guid isPermaLink="true">https://emanharout.com/blog/swifts-guard-statement/</guid><description>The unique characteristics of Swift&apos;s guard statement and how to use it to write cleaner code.</description><pubDate>Wed, 14 Apr 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Swift’s &lt;code&gt;guard&lt;/code&gt; statement is a powerful, well designed feature that promotes clean code when used properly. It is a specialized form of control flow with important advantages. Today we’ll cover the unique characteristics of guard and its proper usage.&lt;/p&gt;
&lt;p&gt;To understand the benefits guard offers, we briefly compare it to its parent, the infamous &lt;code&gt;if&lt;/code&gt; statement. &lt;code&gt;guard&lt;/code&gt; is similar to an &lt;code&gt;if&lt;/code&gt; statement with two important distinctions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Forced Exits:&lt;/strong&gt; guard requires exiting the current scope when the condition is not met.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Remaining in Current Scope:&lt;/strong&gt; guard allows the happy path to proceed in the current scope.
Let’s examine both and how they contribute to cleaner code.&lt;/li&gt;
&lt;/ol&gt;
&lt;section&gt;&lt;h2 id=&quot;forced-exits&quot;&gt;Forced Exits&lt;/h2&gt;&lt;p&gt;Here we have a function that allows customers over 21 entry to the bar.&lt;/p&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#00DAEF&quot;&gt; enterBar&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#00DAEF;--shiki-dark-font-style:italic&quot;&gt;age&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#ACAFFF&quot;&gt;Int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;  guard&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; age &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#FFD493&quot;&gt; 21&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; else&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;    // handle forced exit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    return&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;  // open door&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;  // grant entry&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;  // close door&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Our function is fairly simple. We check if the customer is over 21. If so, we open the door and let them through. If underage, we’re forced to return from the function. There is a good reason for this: guard is guiding us to fail early, preventing further execution of the function. &lt;em&gt;Just as real guards block trespassers, Swift’s guard blocks unwarranted access deeper into the function!&lt;/em&gt;&lt;/p&gt;&lt;p&gt;But why is it good to fail early? It’s about logical conciseness. You wouldn’t open the door to grant entry, then check for ID; it doesn’t make sense to perform conditional actions without checking the condition first—you’re just delaying the potential failure! It’s far better to identify mandatory conditions, and guard against them early. So remember: fail early and often.&lt;/p&gt;&lt;/section&gt;
&lt;section&gt;&lt;h2 id=&quot;remaining-in-current-scope&quot;&gt;Remaining in Current Scope&lt;/h2&gt;&lt;p&gt;So far we’ve discussed what happens when the condition fails, but there’s an advantage with guards upon passing too. Let’s examine:&lt;/p&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;// Scenario A: If&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; age &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#FFD493&quot;&gt; 21&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;  // new scope&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;} &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; age &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#FFD493&quot;&gt; 18&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;  // another new scope&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;// Scenario B: Guard&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;guard&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; age &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#FFD493&quot;&gt; 21&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; else&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;  // new scope only upon failure&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;  return&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-dark:#EEF0F98F;--shiki-dark-font-style:italic&quot;&gt;// passing-flow remains in original scope&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Since &lt;code&gt;if&lt;/code&gt; can check countless conditionals via &lt;code&gt;else if&lt;/code&gt;, each condition has its own nested scope. &lt;code&gt;if&lt;/code&gt; does not understand if a particular condition is a happy path or failure, so it treats them all the same by scoping them all.&lt;/p&gt;&lt;p&gt;Not so with guard. Guard assumes there are two outcomes: failure or passing. It sensibly provides a nested scope only if you fail. When you pass, there’s no newly nested scope at all—you just proceed in the same scope as the guard.&lt;/p&gt;&lt;p&gt;This has important implications. First, this means &lt;code&gt;guard&lt;/code&gt; should be used if you expect one of two outcomes. Second, it improves code clarity by eliminating the need for a nested scope. Since most our time spent as programmers is in code comprehension, this is a welcomed benefit. No one wants an entire function nested—potentially multiple times—just because a mandatory condition must be met.&lt;/p&gt;&lt;p&gt;When you see an entire function is nested because of a mandatory condition, just swap that &lt;code&gt;if&lt;/code&gt; for a &lt;code&gt;guard&lt;/code&gt; instead. Your colleagues (and future self) will thank you.&lt;/p&gt;&lt;/section&gt;
&lt;section&gt;&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;&lt;p&gt;To summarize, &lt;code&gt;guard&lt;/code&gt;s are a specialized form of &lt;code&gt;if&lt;/code&gt;. Guard comes with two important advantages: forced exits on failure, and remaining in current scope upon passing. Guards are to be used when a condition failing means the function can’t carry out its duties, so it’s sensible to identify mandatory conditions and guard against them as early as possible. Guards also help with code clarity by forgoing a nested scope when the condition is met. Hope you all found this helpful!&lt;/p&gt;&lt;/section&gt;</content:encoded><category>programming</category><category>swift</category><author>Eman Harout</author></item><item><title>Swift Solutions: Adapter Pattern</title><link>https://emanharout.com/blog/swift-solutions---adapter-pattern/</link><guid isPermaLink="true">https://emanharout.com/blog/swift-solutions---adapter-pattern/</guid><description>How the Adapter Pattern lets objects with similar functionality work together despite incompatible interfaces.</description><pubDate>Sat, 24 Jun 2017 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The Adapter Pattern is a design pattern that enables objects with similar functionality to work together despite having incompatible interfaces. It allows for integration that results in code that is cleaner and easier to use. Quite literally, it &lt;em&gt;adapts&lt;/em&gt; an object so that it uses more familiar APIs.&lt;/p&gt;
&lt;section&gt;&lt;h2 id=&quot;use-cases&quot;&gt;Use Cases&lt;/h2&gt;&lt;p&gt;The Adapter Pattern should be used when the following are true:&lt;/p&gt;&lt;ul&gt;
&lt;li&gt;A component shares similar functionality with existing objects in your app.&lt;/li&gt;
&lt;li&gt;Despite sharing similar functionality, the component has an interface that is incompatible with other objects in your app. The component is often from a third party framework.&lt;/li&gt;
&lt;li&gt;The component’s source code cannot (or should not) be modified.&lt;/li&gt;
&lt;li&gt;The component needs to integrate with your app.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;The Adapter Pattern allows us to take this foreign object and make it play nice with our existing objects. There are two primary approaches to accomplishing this: Swift Extensions and the Dedicated Adapter Class.&lt;/p&gt;&lt;p&gt;Enough with theory! Let us see what the Adapter Pattern looks like in practice :)&lt;/p&gt;&lt;/section&gt;
&lt;section&gt;&lt;h2 id=&quot;adapter-pattern-swift-extension-approach&quot;&gt;Adapter Pattern: Swift Extension Approach&lt;/h2&gt;&lt;p&gt;The Swift Extension is an elegant solution for most simple scenarios.&lt;/p&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;protocol&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#ACAFFF&quot;&gt; Jumping&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#00DAEF&quot;&gt; jump&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;	&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#ACAFFF&quot;&gt; Dog&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#ACAFFF&quot;&gt;Jumping &lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#00DAEF&quot;&gt; jump&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#EEF0F9&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#FFD493&quot;&gt;&amp;quot;Jumps Excitedly&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;	&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#ACAFFF&quot;&gt; Cat&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#ACAFFF&quot;&gt;Jumping &lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#00DAEF&quot;&gt; jump&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#EEF0F9&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#FFD493&quot;&gt;&amp;quot;Pounces&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; dog &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt; Dog&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; cat &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt; Cat&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here we have a dog and a cat. They are both able to perform &lt;code&gt;jump()&lt;/code&gt;. Now let’s say we integrate a third party framework, and have a foreign animal.&lt;/p&gt;&lt;section&gt;&lt;h3 id=&quot;the-adaptee&quot;&gt;The Adaptee&lt;/h3&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#ACAFFF&quot;&gt; Frog&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#00DAEF&quot;&gt; leap&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#EEF0F9&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#FFD493&quot;&gt;&amp;quot;Leaps&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; frog &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt; Frog&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;A few things to note:&lt;/p&gt;&lt;ol&gt;
&lt;li&gt;Our leaping frog object has some similar functionality with our furry friends.&lt;/li&gt;
&lt;li&gt;Though it jumps, its interface is different: we have to call &lt;code&gt;leap()&lt;/code&gt; instead of &lt;code&gt;jump()&lt;/code&gt; to get the desired functionality.&lt;/li&gt;
&lt;/ol&gt;&lt;/section&gt;&lt;section&gt;&lt;h3 id=&quot;the-adapter&quot;&gt;The Adapter&lt;/h3&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;extension&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#ACAFFF&quot;&gt; Frog&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#ACAFFF&quot;&gt;Jumping &lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#00DAEF&quot;&gt; jump&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;        leap&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here we integrate our component by implementing the Adapter Pattern. We simply conform &lt;code&gt;Frog&lt;/code&gt; to &lt;code&gt;Jumping&lt;/code&gt; and create a wrapper function our other objects recognize. We are now able to get the same behavior out of our frog without modifying its existing implementation. We simply &lt;em&gt;extend&lt;/em&gt; it with a new wrapper function to abstract away its differences! :)&lt;/p&gt;&lt;blockquote&gt;
&lt;p&gt;“Objects should be open for extension, but closed for modification.”&lt;/p&gt;
&lt;/blockquote&gt;&lt;/section&gt;&lt;section&gt;&lt;h3 id=&quot;before-and-after&quot;&gt;Before and After&lt;/h3&gt;&lt;p&gt;It is helpful to see how we would work with our objects before and after we adapted our frog’s interface.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Before:&lt;/strong&gt;&lt;/p&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; animals: [Jumping] &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; [dog, cat]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#00DAEF&quot;&gt; jumpAll&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#00DAEF;--shiki-dark-font-style:italic&quot;&gt;animals&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: [Jumping], &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#00DAEF;--shiki-dark-font-style:italic&quot;&gt;frog&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: Frog &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#FFD493&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; animal &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; animals {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;        animal.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;jump&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; frog &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; frog {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;        frog.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;leap&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here we want to make all our animals jump. Without an implemented adapter, the caller has to have knowledge of the frog’s foreign interface. We cannot treat the component like the rest of our code.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;After:&lt;/strong&gt;&lt;/p&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; animals: [Jumping] &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; [dog, cat, frog]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#00DAEF&quot;&gt; jumpAll&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#00DAEF;--shiki-dark-font-style:italic&quot;&gt;animals&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: [Jumping]) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; animal &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; animals {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;        animal.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;jump&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;With the adapter in place, we can treat the frog like the rest of our objects and include it in our &lt;code&gt;animals&lt;/code&gt; array. We also get to simplify our function by removing the &lt;code&gt;frog:&lt;/code&gt; parameter.&lt;/p&gt;&lt;p&gt;Moreover, we can treat the frog like any other native object by simply calling &lt;code&gt;jump()&lt;/code&gt; on it. The frog is obviously still “leaping” under the hood, but we do not care. The caller no longer needs to have knowledge of &lt;em&gt;how&lt;/em&gt; &lt;code&gt;Frog&lt;/code&gt; jumps.&lt;/p&gt;&lt;/section&gt;&lt;/section&gt;
&lt;section&gt;&lt;h2 id=&quot;adapter-pattern-the-dedicated-adapter-approach&quot;&gt;Adapter Pattern: The Dedicated Adapter Approach&lt;/h2&gt;&lt;p&gt;For more complex scenarios, creating a dedicated adapter class can be helpful.&lt;/p&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light houston&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#eef0f9;--shiki-light-bg:#fff;--shiki-dark-bg:#17191e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#ACAFFF&quot;&gt; FrogAdapter&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#ACAFFF&quot;&gt;Jumping &lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt; let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt; frog &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#EEF0F9&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt; Frog&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#54B9FF&quot;&gt;    func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#00DAEF&quot;&gt; jump&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;        frog.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#00DAEF&quot;&gt;leap&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#EEF0F9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here we create an adapter class that holds the foreign component in a private property.&lt;/p&gt;&lt;p&gt;Extending &lt;code&gt;Frog&lt;/code&gt; may have exposed more than we would have liked. With a dedicated adapter, the caller is no longer able to manipulate the frog directly; it can only use whatever is exposed by &lt;code&gt;FrogAdapter&lt;/code&gt;. This allows for better encapsulation of the frog, giving us complete control over what gets exposed to the caller.&lt;/p&gt;&lt;p&gt;And that is pretty much it for both approaches!&lt;/p&gt;&lt;/section&gt;
&lt;section&gt;&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;&lt;p&gt;The Adapter Pattern freed us from having to accommodate objects with different interfaces through the unification of their APIs. This greatly increased the clarity and simplicity of our code, enabling us to integrate a foreign object with ease. It is a classic, simple pattern that is highly practical in its usage. Hope you enjoyed learning it!&lt;/p&gt;&lt;/section&gt;</content:encoded><category>programming</category><category>swift</category><category>design patterns</category><author>Eman Harout</author></item></channel></rss>